Doxygen
reg::Ex类 参考

Class representing a regular expression. 更多...

#include <regex.h>

class  Private
 Private members of a regular expression 更多...
 

Public 类型

enum  Mode { Mode::RegEx, Mode::Wildcard }
 Matching algorithm 更多...
 

Public 成员函数

 Ex (const std::string &pattern, Mode mode=Mode::RegEx)
 Creates a regular expression object given the pattern as a string. 更多...
 
 ~Ex ()
 Destroys the regular expression object. 更多...
 
bool match (const std::string &str, Match &match, size_t pos=0) const
 Check if a given string matches this regular expression. 更多...
 
bool isValid () const
 

Private 成员函数

 Ex (const Ex &)=delete
 
Exoperator= (const Ex &e)=delete
 

Private 属性

std::unique_ptr< Privatep
 

详细描述

Class representing a regular expression.

It has a similar API as std::regex, but is much faster (and also somewhat more limited).

在文件 regex.h48 行定义.

成员枚举类型说明

◆ Mode

enum reg::Ex::Mode
strong

Matching algorithm

枚举值
RegEx 

full regular expression.

Wildcard 

simple globbing pattern.

在文件 regex.h52 行定义.

构造及析构函数说明

◆ Ex() [1/2]

reg::Ex::Ex ( const std::string &  pattern,
Mode  mode = Mode::RegEx 
)

Creates a regular expression object given the pattern as a string.

Two modes of matching are supported: RegEx and Wildcard

The following special characters are supported in Mode::RegEx mode.

  • c matches character c
  • . matches any character
  • ^ matches the start of the input
  • $ matches the end of the input
  • \< matches the start of a word
  • \> matches the end of a word
  • [] matches a set of characters
  • x* matches a sequence of zero or more x's
  • x+ matches a sequence of one or more x's
  • x? matches an optional x
  • ( matches the start of a capture range
  • ) matches the ends a capture range
  • \c to escape a special character, such as +, [, *, (, etc.
  • \t matches a tab character
  • \n matches a newline character
  • \r matches a return character
  • \s matches any whitespace as defined by std::isspace()
  • \d matches any digit as defined by std::digit()
  • \a matches any alphabetical characters, same as [a-z_A-Z\x80-\xFF]
  • \w matches any alpha numercial character, same as [a-z_A-Z0-9\x80-\xFF]
  • \xHH matches a hexadecimal character, e.g. \xA0 matches character code 160.

A character range can be used to match a character that falls inside a range (or set of ranges). Within the opening [ and closing ] brackets of a character ranges the following is supported:

  • ^ if at the start of the range, a character matches if it is not in the range, e.g. [^\d] matches any character not a digit
  • - when placed between 2 characters it defines a range from the first character to the second. any character that falls in the range will match, e.g. [0-9] matches the digit from 0 to 9.
  • \s, \d, \a, and \w as explained above.
注解
that special characters ., *, ?, $, +, [ do not have a special meaning in a character range. ^ only has a special meaning as the first character.
that capture ranges cannot be nested, and *, +, and ? do not work on capture ranges. e.g. (abd)? is not valid. If multiple capture ranges are specified then some character has to be in between them, e.g. this does not work (.*)(a.*), but this does (.*)a(.*).

In Wildcard mode * is used to match any sequence of zero or more characters. The character ? can be used to match an optional character. Character ranges are also supported, but other characters like $ and + are just treated as literal characters.

在文件 regex.cpp662 行定义.

663  : p(std::make_unique<Private>(mode==Mode::RegEx ? pattern : wildcard2regex(pattern)))
664 {
665  p->compile();
666 #if ENABLE_DEBUG
667  p->dump();
668  assert(!p->error);
669 #endif
670 }

引用了 p.

◆ ~Ex()

reg::Ex::~Ex ( )

Destroys the regular expression object.

Frees resources.

在文件 regex.cpp672 行定义.

673 {
674 }

◆ Ex() [2/2]

reg::Ex::Ex ( const Ex )
privatedelete

成员函数说明

◆ isValid()

bool reg::Ex::isValid ( ) const

在文件 regex.cpp711 行定义.

712 {
713  return !p->pattern.empty() && !p->error;
714 }

引用了 p.

被这些函数引用 getFilterFromList() , 以及 patternMatch().

◆ match()

bool reg::Ex::match ( const std::string &  str,
Match match,
size_t  pos = 0 
) const

Check if a given string matches this regular expression.

参数
strThe input string to match against.
matchThe match object to hold the matching results.
posThe position in the string at which to start the match.
返回
true iff a match is found. Details are stored in the match object.

在文件 regex.cpp676 行定义.

677 {
678  bool found=false;
679  if (p->data.size()==0 || p->error) return found;
680  match.init(&str);
681 
682  PToken tok = p->data[0];
683  if (tok.kind()==PToken::Kind::BeginOfLine) // only test match at the given position
684  {
685  found = p->matchAt(0,str,match,pos,0);
686  }
687  else
688  {
689  if (tok.kind()==PToken::Kind::Character) // search for the start character
690  {
691  size_t index = str.find(tok.asciiValue(),pos);
692  if (index==std::string::npos)
693  {
694  DBG("Ex::match(str='%s',pos=%zu)=false (no start char '%c')\n",str.c_str(),pos,tok.asciiValue());
695  return false;
696  }
697  DBG("pos=%zu str='%s' char='%c' index=%zu\n",index,str.c_str(),tok.asciiValue(),index);
698  pos=index;
699  }
700  while (pos<str.length()) // search for a match starting at pos
701  {
702  found = p->matchAt(0,str,match,pos,0);
703  if (found) break;
704  pos++;
705  }
706  }
707  DBG("Ex::match(str='%s',pos=%zu)=%d\n",str.c_str(),pos,found);
708  return found;
709 }

引用了 reg::PToken::asciiValue(), reg::PToken::BeginOfLine, reg::PToken::Character, DBG, reg::PToken::kind() , 以及 p.

被这些函数引用 reg::match(), reg::Ex::Private::matchAt(), reg::Iterator::operator!=(), reg::replace() , 以及 reg::search().

◆ operator=()

Ex& reg::Ex::operator= ( const Ex e)
privatedelete

类成员变量说明

◆ p

std::unique_ptr<Private> reg::Ex::p
private

在文件 regex.h124 行定义.

被这些函数引用 Ex(), isValid(), match() , 以及 reg::wildcard2regex().


该类的文档由以下文件生成:
DBG
#define DBG(fmt,...)
Definition: regex.cpp:27
reg::PToken::Kind::Character
@ Character
reg::wildcard2regex
static std::string wildcard2regex(const std::string &pattern)
Definition: regex.cpp:617
reg::Ex::match
bool match(const std::string &str, Match &match, size_t pos=0) const
Check if a given string matches this regular expression.
Definition: regex.cpp:676
reg::PToken::Kind::BeginOfLine
@ BeginOfLine
reg::Ex::Mode::RegEx
@ RegEx
full regular expression.
reg::Ex::p
std::unique_ptr< Private > p
Definition: regex.h:124