PTLib  Version 2.18.8
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
PRegularExpression Class Reference

A class representing a regular expression that may be used for locating patterns in strings. More...

#include <pstring.h>

Inheritance diagram for PRegularExpression:
Collaboration diagram for PRegularExpression:

Public Member Functions

Constructors & destructors
 P_DECLARE_BITWISE_ENUM (CompileOptions, 3,(Simple, Extended, IgnoreCase, AnchorNewLine))
 Flags for compiler options. More...
 
 P_DECLARE_BITWISE_ENUM (ExecOptions, 2,(Normal, NotBeginningOfLine, NotEndofLine))
 Flags for execution options. More...
 
 PRegularExpression ()
 Create a new, empty, regular expression. More...
 
 PRegularExpression (const PString &pattern, CompileOptions options=IgnoreCase)
 Create and compile a new regular expression pattern. More...
 
 PRegularExpression (const char *cpattern, CompileOptions options=IgnoreCase)
 Create and compile a new regular expression pattern. More...
 
 PRegularExpression (const PRegularExpression &)
 Copy a regular expression. More...
 
PRegularExpressionoperator= (const PRegularExpression &)
 Assign a regular expression. More...
 
 ~PRegularExpression ()
 Release storage for the compiled regular expression. More...
 
Overrides from class PObject
virtual void PrintOn (ostream &strm) const
 Output the regular expression to the specified stream. More...
 
Compile & Execute functions
bool Compile (const PString &pattern, CompileOptions options=IgnoreCase)
 Compiler pattern. More...
 
bool Compile (const char *cpattern, CompileOptions options=IgnoreCase)
 Compiler pattern. More...
 
bool Execute (const PString &str, PINDEX &start, ExecOptions options=Normal) const
 Execute regular expression. More...
 
bool Execute (const PString &str, PINDEX &start, PINDEX &len, ExecOptions options=Normal) const
 Execute regular expression. More...
 
bool Execute (const char *cstr, PINDEX &start, ExecOptions options=Normal) const
 Execute regular expression. More...
 
bool Execute (const char *cstr, PINDEX &start, PINDEX &len, ExecOptions options=Normal) const
 Execute regular expression. More...
 
bool Execute (const PString &str, PIntArray &starts, ExecOptions options=Normal) const
 Execute regular expression. More...
 
bool Execute (const PString &str, PIntArray &starts, PIntArray &ends, ExecOptions options=Normal) const
 Execute regular expression. More...
 
bool Execute (const char *cstr, PIntArray &starts, ExecOptions options=Normal) const
 Execute regular expression. More...
 
bool Execute (const char *cstr, PIntArray &starts, PIntArray &ends, ExecOptions options=Normal) const
 Execute regular expression. More...
 
bool Execute (const char *cstr, PStringArray &substring, ExecOptions options=Normal) const
 Execute regular expression. More...
 
- Public Member Functions inherited from PObject
__inline unsigned GetTraceContextIdentifier () const
 Get PTRACE context identifier. More...
 
__inline void SetTraceContextIdentifier (unsigned id)
 
__inline void SetTraceContextIdentifier (const PObject &obj)
 
__inline void SetTraceContextIdentifier (const PObject *obj)
 
__inline void CopyTraceContextIdentifier (PObject &obj) const
 
__inline void CopyTraceContextIdentifier (PObject *obj) const
 
virtual ~PObject ()
 
__inline const char * GetClass () const
 
__inline bool IsClass (const char *name) const
 
__inline const PObjectPTraceObjectInstance () const
 
virtual PObjectClone () const
 Create a copy of the class on the heap. More...
 
template<class CLS >
CLS * CloneAs () const
 As for Clone() but converts to specified type. More...
 
virtual PINDEX HashFunction () const
 This function yields a hash value required by the PDictionary class. More...
 
virtual Comparison Compare (const PObject &obj) const
 Compare the two objects and return their relative rank. More...
 
virtual Comparison CompareObjectMemoryDirect (const PObject &obj) const
 Determine the byte wise comparison of two objects. More...
 
bool operator== (const PObject &obj) const
 Compare the two objects. More...
 
bool operator!= (const PObject &obj) const
 Compare the two objects. More...
 
bool operator< (const PObject &obj) const
 Compare the two objects. More...
 
bool operator> (const PObject &obj) const
 Compare the two objects. More...
 
bool operator<= (const PObject &obj) const
 Compare the two objects. More...
 
bool operator>= (const PObject &obj) const
 Compare the two objects. More...
 
virtual void PrintOn (ostream &strm) const
 Output the contents of the object to the stream. More...
 
virtual void ReadFrom (istream &strm)
 Input the contents of the object from the stream. More...
 

Static Public Member Functions

Miscellaneous functions
static PString EscapeString (const PString &str)
 Escape all characters in the str parameter that have a special meaning within a regular expression. More...
 
- Static Public Member Functions inherited from PObject
static __inline void CopyTraceContextIdentifier (PObject &to, const PObject &from)
 
static __inline void CopyTraceContextIdentifier (PObject &to, const PObject *from)
 
static __inline void CopyTraceContextIdentifier (PObject *to, const PObject &from)
 
static __inline void CopyTraceContextIdentifier (PObject *to, const PObject *from)
 
static __inline const char * Class ()
 
static __inline const PObjectPTraceObjectInstance (const char *)
 
static __inline const PObjectPTraceObjectInstance (const PObject *obj)
 
template<typename T >
static Comparison Compare2 (T v1, T v2)
 Compare two types, returning Comparison type. More...
 
static Comparison InternalCompareObjectMemoryDirect (const PObject *obj1, const PObject *obj2, PINDEX size)
 Internal function caled from CompareObjectMemoryDirect() More...
 

Protected Member Functions

bool InternalCompile (bool assertOnFail)
 
void InternalClean ()
 
- Protected Member Functions inherited from PObject
 PObject ()
 Constructor for PObject, made protected so cannot ever create one on its own. More...
 

Protected Attributes

PString m_pattern
 
CompileOptions m_compileOptions
 
void * m_compiledRegex
 
- Protected Attributes inherited from PObject
unsigned m_traceContextIdentifier
 

Status functions

enum  ErrorCodes {
  NoError = 0, NoMatch, BadPattern, CollateError,
  BadClassType, BadEscape, BadSubReg, UnmatchedBracket,
  UnmatchedParen, UnmatchedBrace, BadBR, RangeError,
  OutOfMemory, BadRepitition, PrematureEnd, TooBig,
  UnmatchedRParen, NotCompiled
}
 Error codes. More...
 
ErrorCodes GetErrorCode () const
 Get the error code for the last Compile() or Execute() operation. More...
 
PString GetErrorText () const
 Get the text description for the error of the last Compile() or Execute() operation. More...
 
const PStringGetPattern () const
 Return the string which represents the pattern matched by the regular expression. More...
 

Detailed Description

A class representing a regular expression that may be used for locating patterns in strings.

The regular expression string is "compiled" into a form that is more efficient during the matching. This compiled form exists for the lifetime of the PRegularExpression instance.

Member Enumeration Documentation

Error codes.

Enumerator
NoError 

Success.

NoMatch 

Didn't find a match (for regexec).

BadPattern 

Invalid pattern.

CollateError 

Not implemented.

BadClassType 

Invalid character class name.

BadEscape 

Trailing backslash.

BadSubReg 

Invalid back reference.

UnmatchedBracket 

Unmatched left bracket.

UnmatchedParen 

Parenthesis imbalance.

UnmatchedBrace 

Unmatched \.

BadBR 

Invalid contents of \.

RangeError 

Invalid range end.

OutOfMemory 

Ran out of memory.

BadRepitition 

No preceding re for repetition op.

PrematureEnd 

Premature end.

TooBig 

Compiled pattern bigger than 2^16 bytes.

UnmatchedRParen 

Unmatched ) or \); not returned from regcomp.

NotCompiled 

Miscellaneous error.

Constructor & Destructor Documentation

PRegularExpression::PRegularExpression ( )

Create a new, empty, regular expression.

PRegularExpression::PRegularExpression ( const PString pattern,
CompileOptions  options = IgnoreCase 
)

Create and compile a new regular expression pattern.

Parameters
patternPattern to compile
optionsPattern match options
PRegularExpression::PRegularExpression ( const char *  cpattern,
CompileOptions  options = IgnoreCase 
)

Create and compile a new regular expression pattern.

Parameters
cpatternPattern to compile
optionsPattern match options
PRegularExpression::PRegularExpression ( const PRegularExpression )

Copy a regular expression.

PRegularExpression::~PRegularExpression ( )

Release storage for the compiled regular expression.

Member Function Documentation

bool PRegularExpression::Compile ( const PString pattern,
CompileOptions  options = IgnoreCase 
)

Compiler pattern.

Parameters
patternPattern to compile
optionsPattern match options
bool PRegularExpression::Compile ( const char *  cpattern,
CompileOptions  options = IgnoreCase 
)

Compiler pattern.

The pattern is compiled into an internal format to speed subsequent execution of the pattern match algorithm.

Returns
true if successfully compiled.
Parameters
cpatternPattern to compile
optionsPattern match options
static PString PRegularExpression::EscapeString ( const PString str)
static

Escape all characters in the str parameter that have a special meaning within a regular expression.

Returns
String with additional escape ('\') characters.
Parameters
strString to add esacpes to.
bool PRegularExpression::Execute ( const PString str,
PINDEX &  start,
ExecOptions  options = Normal 
) const

Execute regular expression.

Parameters
strSource string to search
startFirst match location
optionsPattern match options
bool PRegularExpression::Execute ( const PString str,
PINDEX &  start,
PINDEX &  len,
ExecOptions  options = Normal 
) const

Execute regular expression.

Parameters
strSource string to search
startFirst match location
lenLength of match
optionsPattern match options
bool PRegularExpression::Execute ( const char *  cstr,
PINDEX &  start,
ExecOptions  options = Normal 
) const

Execute regular expression.

Parameters
cstrSource string to search
startFirst match location
optionsPattern match options
bool PRegularExpression::Execute ( const char *  cstr,
PINDEX &  start,
PINDEX &  len,
ExecOptions  options = Normal 
) const

Execute regular expression.

Parameters
cstrSource string to search
startFirst match location
lenLength of match
optionsPattern match options
bool PRegularExpression::Execute ( const PString str,
PIntArray starts,
ExecOptions  options = Normal 
) const

Execute regular expression.

Parameters
strSource string to search
startsArray of match locations
optionsPattern match options
bool PRegularExpression::Execute ( const PString str,
PIntArray starts,
PIntArray ends,
ExecOptions  options = Normal 
) const

Execute regular expression.

Parameters
strSource string to search
startsArray of match locations
endsArray of match ends
optionsPattern match options
bool PRegularExpression::Execute ( const char *  cstr,
PIntArray starts,
ExecOptions  options = Normal 
) const

Execute regular expression.

Parameters
cstrSource string to search
startsArray of match locations
optionsPattern match options
bool PRegularExpression::Execute ( const char *  cstr,
PIntArray starts,
PIntArray ends,
ExecOptions  options = Normal 
) const

Execute regular expression.

Execute the pattern match algorithm using the previously compiled pattern.

The starts and ends arrays indicate the start and end positions of the matched substrings in cstr. The 0th member is filled in to indicate what substring was matched by the entire regular expression. The remaining members report what substring was matched by parenthesized subexpressions within the regular expression.

The caller should set the size of the starts array before calling to indicated how many subexpressions are expected. If the starts array is empty, it will be set to one entry.

Returns
true if successfully compiled.
Parameters
cstrSource string to search
startsArray of match locations
endsArray of match ends
optionsPattern match options
bool PRegularExpression::Execute ( const char *  cstr,
PStringArray substring,
ExecOptions  options = Normal 
) const

Execute regular expression.

Execute the pattern match algorithm using the previously compiled pattern.

The substring arrays is filled with the matched substrings out of cstr. The 0th member is filled in to indicate what substring was matched by the entire regular expression. The remaining members report what substring was matched by parenthesized subexpressions within the regular expression.

The caller should set the size of the substring array before calling to indicated how many subexpressions are expected. If the substring array is empty, it will be set to one entry.

Returns
true if successfully compiled.
Parameters
cstrSource string to search
substringArray of matched substrings
optionsPattern match options
ErrorCodes PRegularExpression::GetErrorCode ( ) const

Get the error code for the last Compile() or Execute() operation.

Returns
Error code.
PString PRegularExpression::GetErrorText ( ) const

Get the text description for the error of the last Compile() or Execute() operation.

Returns
Error text string.
const PString& PRegularExpression::GetPattern ( ) const
inline

Return the string which represents the pattern matched by the regular expression.

References m_pattern.

void PRegularExpression::InternalClean ( )
protected
bool PRegularExpression::InternalCompile ( bool  assertOnFail)
protected
PRegularExpression& PRegularExpression::operator= ( const PRegularExpression )

Assign a regular expression.

PRegularExpression::P_DECLARE_BITWISE_ENUM ( CompileOptions  ,
,
(Simple, Extended, IgnoreCase, AnchorNewLine)   
)

Flags for compiler options.

PRegularExpression::P_DECLARE_BITWISE_ENUM ( ExecOptions  ,
,
(Normal, NotBeginningOfLine, NotEndofLine)   
)

Flags for execution options.

virtual void PRegularExpression::PrintOn ( ostream &  strm) const
virtual

Output the regular expression to the specified stream.

Parameters
strmI/O stream to output to.

Member Data Documentation

void* PRegularExpression::m_compiledRegex
protected
CompileOptions PRegularExpression::m_compileOptions
protected
PString PRegularExpression::m_pattern
protected

Referenced by GetPattern().


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