Skip to main content

The Ex Class Reference

Class representing a regular expression. More...

Declaration

class reg::Ex { ... }

Included Headers

#include <src/regex.h>

Enumerations Index

enum classMode { ... }

Matching algorithm. More...

Public Constructors Index

Ex (std::string_view pattern, Mode mode=Mode::RegEx)

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

Public Destructor Index

~Ex ()

Destroys the regular expression object. More...

Public Member Functions Index

boolmatch (std::string_view str, Match &match, size_t pos=0) const

Check if a given string matches this regular expression. More...

boolisValid () const

Private Member Attributes Index

std::unique_ptr< Private >p

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.

Enumerations

Mode

enum class reg::Ex::Mode
strong

Matching algorithm.

Enumeration values
RegExfull regular expression
Wildcardsimple globbing pattern

Definition at line 42 of file regex.h.

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

Public Constructors

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.
info

that special characters ., *, ?, $, +, [ do not have a special meaning in a character range. ^ only has a special meaning as the first character.

info

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.

Declaration at line 97 of file regex.h, definition at line 694 of file regex.cpp.

694Ex::Ex(std::string_view pattern, Mode mode)
695 : p(std::make_unique<Private>(mode==Mode::RegEx ? pattern : wildcard2regex(pattern)))
696{
697 p->compile();
698#if ENABLE_DEBUG
699 p->dump();
700 assert(!p->error);
701#endif
702}

References p, RegEx and reg::wildcard2regex.

Referenced by ~Ex.

Public Destructor

~Ex()

reg::Ex::~Ex ()

Destroys the regular expression object.

Frees resources.

Definition at line 100 of file regex.h.

References Ex and match.

Public Member Functions

isValid()

bool reg::Ex::isValid ()

Declaration at line 109 of file regex.h, definition at line 741 of file regex.cpp.

741bool Ex::isValid() const
742{
743 return !p->pattern.empty() && !p->error;
744}

Reference p.

Referenced by genericPatternMatch and getFilterFromList.

match()

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

Check if a given string matches this regular expression.

Parameters
str

The input string to match against.

match

The match object to hold the matching results.

pos

The position in the string at which to start the match.

Returns

true iff a match is found. Details are stored in the match object.

Declaration at line 108 of file regex.h, definition at line 706 of file regex.cpp.

706bool Ex::match(std::string_view str,Match &match,size_t pos) const
707{
708 bool found=false;
709 if (p->data.size()==0 || p->error) return found;
710 match.init(str);
711
712 PToken tok = p->data[0];
713 if (tok.kind()==PToken::Kind::BeginOfLine) // only test match at the given position
714 {
715 found = p->matchAt(0,p->data.size(),str,match,pos,0);
716 }
717 else
718 {
719 if (tok.kind()==PToken::Kind::Character) // search for the start character
720 {
721 size_t index = str.find(tok.asciiValue(),pos);
722 if (index==std::string::npos)
723 {
724 DBG("Ex::match(str='%s',pos=%zu)=false (no start char '%c')\n",str.c_str(),pos,tok.asciiValue());
725 return false;
726 }
727 DBG("pos=%zu str='%s' char='%c' index=%zu\n",index,str.c_str(),tok.asciiValue(),index);
728 pos=index;
729 }
730 while (pos<str.length()) // search for a match starting at pos
731 {
732 found = p->matchAt(0,p->data.size(),str,match,pos,0);
733 if (found) break;
734 pos++;
735 }
736 }
737 DBG("Ex::match(str='%s',pos=%zu)=%d\n",str.c_str(),pos,found);
738 return found;
739}

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

Referenced by match, reg::match, reg::match, reg::Ex::Private::matchAt, reg::replace, reg::search, reg::search and ~Ex.

Private Member Attributes

p

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

Definition at line 114 of file regex.h.

114 std::unique_ptr<Private> p;

Referenced by Ex, isValid and match.


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


Generated via doxygen2docusaurus by Doxygen 1.14.0.