6.4 Regexp

The Regexp class stores and handles regular expressions. This class is a container and a processor. Regular expressions are probably the most advanced way of matching strings or string parts. But they are also hard to write for the beginner. It is beyond the scope of this manual to describe how to write regular expressions. The regex (often in section 7) manual (man regex on Unix) is a start to learn regular expressions. Entire books have been written covering regular expressions, and the Net contains numerous pages about the subject. Go to your preferred search engine or book store and get the knowledge.

Actually most computer users have written regular expressions one time or the other, especially to get only file names matching a given criterion in a directory. For example "*.txt" (find all files ending in .txt) is a regular expression. But many more elaborated expressions can be written. A good documentation about regular expressions will explain you what you can make with the Regexp class.

Despite the fact that String can store multi-byte symbols, the Regexp class can only handle single-byte symbol strings. Indeed the implementation uses the C-library regular expression. This will change as soon as a portable multi-byte regular expression code is written by someone and we get knowledge of.


Synopsis


  #include <lyric/Regexp.hpp>
  
  class Regexp
  {
  public:
    ~Regexp ();
    Regexp ();
    Regexp (const Regexp::Property& property);
    Regexp (const String& expression)
      throw (Exception::Regexp::Invalid, Exception::Memory::Alloc);
    Regexp (const Regexp::Property& property, const String& expression)
      throw (Exception::Regexp::Invalid, Exception::Memory::Alloc);
    Regexp (const Regexp& regexp)
      throw (Exception::Memory::Alloc);
    Regexp& operator = (const Regexp& regexp)
      throw (Exception::Memory::Alloc);
    Regexp& operator << (const String& expression)
      throw (Exception::Memory::Alloc);
    bool match (const String& test) const;
    List<String::SubId> matches (const String& string) const
      throw (Exception::Memory::Alloc);
  
  friend:
    String& operator << (String& out, const Regexp& regexp)
      throw (Exception::Memory::Alloc);
  };


Description


˜Regexp ()
Destroys this regular expression. Releases all used memory resources.

Regexp ()
Constructs this regular expression. Sets to the default property (extended syntax, differentiate case).

Regexp (const Regexp::Property& property)
Constructs this regular expression with the given property. See 6.4.1 for the Regexp::Property class documentation.

Regexp (const String& expression)
Constructs this regular expression as the given expression. Sets the property to the default extended syntax, differentiate case. The given expression is expected to be of extended syntax.
|\ Exception::Regexp::Invalid
is thrown if the given expression is not of extended syntax.
|\ Exception::Memory::Alloc
is thrown if not enough memory was found to store the regular expression, either in plain text or in compiled format.

Regexp (const Regexp::Property& property, const String& expression)
Constructs this regular expression as the given expression, the later being interpreted with the given property. The given expression must match the syntax defined in the given property.
|\ Exception::Regexp::Invalid
is thrown if the given expression is not of of the syntax defined in the given property.
|\ Exception::Memory::Alloc
is thrown if not enough memory was found to store the regular expression, either in plain text or in compiled format.

Regexp (const Regexp& regexp)
Constructs this regular expression from the given regexp. All data and properties stored in regexp are copied into this regular expression.
|\ Exception::Memory::Alloc
is thrown if not enough memory was found to store the regular expression, either in plain text or in compiled format.

Regexp& operator = (const Regexp& expression)
Assigns the given expression to this regular expression. A reference to this regular expression is returned for assignment chaining operations. All data and properties stored in regexp are copied into this regular expression.
|\ Exception::Memory::Alloc
is thrown if not enough memory was found to store the regular expression, either in plain text or in compiled format.

Regexp& operator << (const String& expression)
Sets this regular expression to the given plain text expression. The given plain text expression must match the syntax defined for this regular expression during construction. A reference to this regular expression is returned for assignment operation chaining.
|\ Exception::Regexp::Invalid
is thrown if the given expression is not of of the syntax defined in the properties of this regular expression.
|\ Exception::Memory::Alloc
is thrown if not enough memory was found to store the regular expression, either in plain text or in compiled format.

bool match (const String& test) const
Returns true if the given test string matches this regular expression, false if not.

List<String::SubId> matches (const String& string) const
Returns the list of sub-string identificators (see 2.9.1) where the given string matches this regular expression.
|\ Exception::Memory::Alloc
is thrown if not enough memory was found to store the list of sub-string identificators.

String& operator << (String& out, Regexp& regexp)
Writes the regular expression regexp in plain text into the output string. This operation is used for regular expression I/O. A reference to the output string is returned for String << operations chaining.
|\ Exception::Memory::Alloc
is thrown if not enough memory was found to store the output string.


  6.4.1 Regexp::Property