edelib: Regex Class Reference

edelib 1.0

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- Class List
- Class Hierarchy
- Class Members

Public Types | Public Member Functions | List of all members
Regex Class Reference

Regex class. More...

`#include <edelib/Regex.h>`

## Public Types
typedef list< RegexMatch > | MatchVec

## Public Member Functions
| Regex ()
| ~Regex ()
bool | compile (const char *pattern, int m=0)
| operator bool (void) const
int | match (const char *str, int match_mode, int start, int len, MatchVec *matches)
int | match (const char *str, int match_mode=0, MatchVec *matches=0)
int | split (const char *str, list< String > &ls, int match_mode=0)
const char * | strerror (void) const

## Detailed Description

Regex class.

Regex is pattern matching (aka. regular expression) class used to search certain substrings inside line or text. This class use Perl compatible expressions thanks to PCRE backend.

Before doing some matching, you must compile pattern first buy using compile() member.

Matching is done via match() member. It will return the number of matched substrings and (if given) set RegexMatch list with offsets. If fails, -1 will be returned. Here is the sample: that explains it:

Regex r;
Regex::MatchVec mv;
r.compile("(de|br)mo");
r.match("some demo string", 0, &mv);

Here, match() will return 2 and MatchVec list will have (5,4) and (5,2) values (where the first value is offset and the second substring length). This explains one important property inherited from PCRE: when grouped patterns are involved and they are found in string, MatchVec first element will be full matched string and substrings will follow.

## Member Typedef Documentation

typedef list<RegexMatch> MatchVec

Shortcut for the list of RegexMatch

## Constructor & Destructor Documentation

Regex | ( |  | ) |

Empty constructor

~Regex | ( |  | ) |

Clears all internal data

## Member Function Documentation

bool compile | ( | const char * | pattern,
|  | int | m = `0`
| ) |  |

Compile pattern for matching. This must be called before match/search functions.

Returns
  true if pattern compiled successfully, or false if not.

Parameters

  pattern | is matching pattern
  m | is RegexMode type

int match | ( | const char * | str,
|  | int | match_mode,
|  | int | start,
|  | int | len,
|  | MatchVec * | matches
| ) |  |

Search compiled pattern in str and return number of grouped matches. If pattern wasn't found or some error occured during search, returned value (depending on error) will be less than 0 (see PCRE documentation about returned values).

Returns
  the number of grouped matches or value less than 0 in case of error

Parameters

  str | is target string where compiled pattern is searched
  match_mode | is OR-ed RegexMatchMode value
  start | is starting position on string
  len | is desired length where matching will occur; if given -1, full length will be searched
  matches | is list of matching objects

int match | ( | const char * | str,
|  | int | match_mode = `0`,
|  | MatchVec * | matches = `0`
| ) |  |
| inline

The same as above match(), but matching will start from the beginning and full string length will be used

operator bool | ( | void |  | ) | const

Validate if compile() was successfull. This is short way for checking compile() returned value, like:

Regex r;
r.compile(...);
if(!r)
// die

int split | ( | const char * | str,
|  | list< String > & | ls,
|  | int | match_mode = `0`
| ) |  |

Split given str and put each of splitted items in list.

Returns
  0 if pattern didn't find or size of the list

Parameters

  str | is data to be splitted
  ls | is list where to put data
  match_mode | is OR-ed RegexMatchMode value

const char* strerror | ( | void |  | ) | const

Return error in string form. Returned value points to static data and must not be modified or cleared

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

- edelib/Regex.h