Doxygen
Loading...
Searching...
No Matches
reg::Ex Class Reference

Class representing a regular expression. More...

#include <src/regex.h>

Classes

class  Private
 Private members of a regular expression. More...

Public Types

enum class  Mode { RegEx , Wildcard }
 Matching algorithm. More...

Public Member Functions

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

Private Attributes

std::unique_ptr< Privatep

Detailed Description

Class representing a regular expression.

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

Definition at line 38 of file regex.h.

Member Enumeration Documentation

◆ Mode

enum class reg::Ex::Mode
strong

Matching algorithm.

Enumerator
RegEx 

full regular expression.

Wildcard 

simple globbing pattern.

Definition at line 42 of file regex.h.

43 {
44 RegEx, /**< full regular expression. */
45 Wildcard /**< simple globbing pattern. */
46 };

Constructor & Destructor Documentation

◆ Ex()

reg::Ex::Ex ( std::string_view 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 numerical 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.
Note
that special characters ., *, ?, $, +, [ do not have a special meaning in a character range. ^ only has a special meaning as the first character.
capture ranges can be nested. Optional capture ranges (...)? are supported but repeated ranges (...)* or (...)+ are not.

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.

Definition at line 793 of file regex.cpp.

794 : p(std::make_unique<Private>(mode==Mode::RegEx ? pattern : wildcard2regex(pattern)))
795{
796 p->compile();
797#if ENABLE_DEBUG
798 p->dump();
799 assert(!p->error);
800#endif
801}
std::unique_ptr< Private > p
Definition regex.h:112
@ RegEx
full regular expression.
Definition regex.h:44
static std::string wildcard2regex(std::string_view pattern)
Definition regex.cpp:748

References p, and reg::wildcard2regex().

Referenced by ~Ex().

◆ ~Ex()

reg::Ex::~Ex ( )
default

Destroys the regular expression object.

Frees resources.

References Ex(), isValid(), match(), and NON_COPYABLE.

Member Function Documentation

◆ isValid()

bool reg::Ex::isValid ( ) const

Definition at line 840 of file regex.cpp.

841{
842 return !p->pattern.empty() && !p->error;
843}

References p.

Referenced by genericPatternMatch(), getFilterFromList(), and ~Ex().

◆ match()

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

Check if a given string matches this regular expression.

Parameters
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.
Returns
true iff a match is found. Details are stored in the match object.

Definition at line 805 of file regex.cpp.

806{
807 bool found=false;
808 if (p->data.size()==0 || p->error) return found;
809 match.init(str,p->captureCount);
810
811 PToken tok = p->data[0];
812 if (tok.kind()==PToken::Kind::BeginOfLine) // only test match at the given position
813 {
814 found = p->matchAt(0,p->data.size(),str,match,pos,0);
815 }
816 else
817 {
818 if (tok.kind()==PToken::Kind::Character) // search for the start character
819 {
820 size_t index = str.find(tok.asciiValue(),pos);
821 if (index==std::string::npos)
822 {
823 DBG("Ex::match(str='%s',pos=%zu)=false (no start char '%c')\n",std::string(str).c_str(),pos,tok.asciiValue());
824 return false;
825 }
826 DBG("pos=%zu str='%s' char='%c' index=%zu\n",index,std::string(str).c_str(),tok.asciiValue(),index);
827 pos=index;
828 }
829 while (pos<str.length()) // search for a match starting at pos
830 {
831 found = p->matchAt(0,p->data.size(),str,match,pos,0);
832 if (found) break;
833 pos++;
834 }
835 }
836 DBG("Ex::match(str='%s',pos=%zu)=%d\n",std::string(str).c_str(),pos,found);
837 return found;
838}
bool match(std::string_view str, Match &match, size_t pos=0) const
Check if a given string matches this regular expression.
Definition regex.cpp:805
#define DBG(x)
Definition dotrunner.cpp:70

References reg::PToken::asciiValue(), reg::PToken::BeginOfLine, reg::PToken::Character, DBG, reg::PToken::kind(), match(), and p.

Referenced by reg::Iterator::findNext(), match(), reg::match(), reg::match(), reg::Ex::Private::matchAt(), reg::replace(), reg::search(), reg::search(), and ~Ex().

Member Data Documentation

◆ p

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

Definition at line 112 of file regex.h.

Referenced by Ex(), isValid(), and match().


The documentation for this class was generated from the following files: