D Programming Language 2.0

Last update Wed Aug 1 09:43:29 2012

# std.regexp

Deprecated. Please use std.regex instead.

Regular expressions are a powerful method of string pattern matching. The regular expression language used in this library is the same as that commonly used, however, some of the very advanced forms may behave slightly differently. The standard observed is the ECMA standard for regular expressions.

std.regexp is designed to work only with valid UTF strings as input. To validate untrusted input, use std.utf.validate().

In the following guide, pattern[] refers to a regular expression. The attributes[] refers to a string controlling the interpretation of the regular expression. It consists of a sequence of one or more of the following characters:

Attribute Characters
Attribute | Action
--- | ---
g | global; repeat over the whole input string
i | case insensitive
m | treat as multiple lines separated by newlines

The format[] string has the formatting characters:

Formatting Characters
Format | Replaced With
--- | ---
$$ | $
$& | The matched substring.
$` | The portion of string that precedes the matched substring.
$' | The portion of string that follows the matched substring.
$n | The nth capture, where n is a single digit 1-9 and n is not followed by a decimal digit.
$nn | The nnth capture, where nn is a two-digit decimal number 01-99. If nnth capture is undefined or more than the number of parenthesized subexpressions, use the empty string instead.

Any other $ are left as is.

References:
Wikipedia

License:
Boost License 1.0.

Authors:
Walter Bright

Source:
std/regexp.d

deprecated string email;
  Regular expression to extract an email address.

  References:
  How to Find or Validate an Email Address
  RFC 2822 Internet Message Format

deprecated string url;
  Regular expression to extract a url

class RegExpException: object.Exception;
  One of these gets thrown on compilation errors

deprecated string sub(string s, string pattern, string format, string attributes = null);
  Search string for matches with regular expression pattern with attributes. Replace each match with string generated from format.

  Parameters:

  string s | String to search.
  string pattern | Regular expression pattern.
  string format | Replacement string format.
  string attributes | Regular expression attributes.

  Returns:
  the resulting string

  Example:
  Replace the letters 'a' with the letters 'ZZ'.

       s = "Strap a rocket engine on a chicken."
       sub(s, "a", "ZZ")        // result: StrZZp a rocket engine on a chicken.
       sub(s, "a", "ZZ", "g")   // result: StrZZp ZZ rocket engine on ZZ chicken.

  The replacement format can reference the matches using the $&, $$, $', $`, .. 9 notation:

       sub(s, "[ar]", "[$&]", "g") // result: St[r][a]p [a] [r]ocket engine on [a] chi

deprecated string sub(string s, string pattern, string delegate(RegExp) dg, string attributes = null);
  Search string for matches with regular expression pattern with attributes. Pass each match to delegate dg. Replace each match with the return value from dg.

  Parameters:

  string s | String to search.
  string pattern | Regular expression pattern.
  string delegate(RegExp) dg | Delegate
  string attributes | Regular expression attributes.

  Returns:
  the resulting string.

  Example:
  Capitalize the letters 'a' and 'r':

       s = "Strap a rocket engine on a chicken.";
       sub(s, "[ar]",
          delegate char[] (RegExp m)
          {
               return toUpper(m[0]);
          },
          "g");    // result: StRAp A Rocket engine on A chicken.

deprecated sizediff_t find(string s, RegExp pattern);
  Search s[] for first match with pattern.

  Parameters:

  string s | String to search.
  RegExp pattern | Regular expression pattern.

  Returns:
  index into s[] of match if found, -1 if no match.

  Example:

       auto s = "abcabcabab";
       find(s, RegExp("b"));    // match, returns 1
       find(s, RegExp("f"));    // no match, returns -1

deprecated sizediff_t find(string s, string pattern, string attributes = null);
  Returns:
  Same as find(s, RegExp(pattern, attributes)).

  WARNING:
  This function is scheduled for deprecation due to unnecessary ambiguity with the homonym function in std.string. Instead of std.regexp.find(s, p, a), you may want to use find(s, RegExp(p, a)).

deprecated sizediff_t rfind(string s, RegExp pattern);
  Search s[] for last match with pattern.

  Parameters:

  string s | String to search.
  RegExp pattern | Regular expression pattern.

  Returns:
  index into s[] of match if found, -1 if no match.

  Example:

       auto s = "abcabcabab";
       rfind(s, RegExp("b"));    // match, returns 9
       rfind(s, RegExp("f"));    // no match, returns -1

deprecated sizediff_t rfind(string s, string pattern, string attributes = null);
  Returns:
  Same as rfind(s, RegExp(pattern, attributes)).

  WARNING:
  This function is scheduled for deprecation due to unnecessary ambiguity with the homonym function in std.string. Instead of std.regexp.rfind(s, p, a), you may want to use rfind(s, RegExp(p, a)).

deprecated string[] split(string s, RegExp pattern);
  Split s[] into an array of strings, using the regular expression pattern as the separator.

  Parameters:

  string s | String to search.
  RegExp pattern | Regular expression pattern.

  Returns:
  array of slices into s[]

  Example:

       foreach (s; split("abcabcabab", RegExp("C.", "i")))
       {
           writefln("s = '%s'", s);
       }
       // Prints:
       // s = 'ab'
       // s = 'b'
       // s = 'bab'

deprecated string[] split(string s, string pattern, string attributes = null);
  Returns:
  Same as split(s, RegExp(pattern, attributes)).

  WARNING:
  This function is scheduled for deprecation due to unnecessary ambiguity with the homonym function in std.string. Instead of std.regexp.split(s, p, a), you may want to use split(s, RegExp(p, a)).

deprecated RegExp search(string s, string pattern, string attributes = null);
  Search s[] for first match with pattern[] with attributes[].

  Parameters:

  string s | String to search.
  string pattern | Regular expression pattern.
  string attributes | Regular expression attributes.

  Returns:
  corresponding RegExp if found, null if not.

  Example:

       import std.stdio;
       import std.regexp;

       void main()
       {
           if (auto m = std.regexp.search("abcdef", "c"))
           {
               writefln("%s[%s]%s", m.pre, m[0], m.post);
           }
       }
       // Prints:
       // ab[c]def

class RegExp;
  RegExp is a class to handle regular expressions.

  It is the core foundation for adding powerful string pattern matching capabilities to programs like grep, text editors, awk, sed, etc.

  this(string pattern, string attributes = null);
    Construct a RegExp object. Compile pattern with attributes into an internal form for fast execution.

    Parameters:

    string pattern | regular expression
    string attributes | attributes

    Throws:
    RegExpException if there are any compilation errors.

    Example:
    Declare two variables and assign to them a RegExp object:

         auto r = new RegExp("pattern");
         auto s = new RegExp(r"p[1-5]\s*");

  static RegExp opCall(string pattern, string attributes = null);
    Generate instance of RegExp.

    Parameters:

    string pattern | regular expression
    string attributes | attributes

    Throws:
    RegExpException if there are any compilation errors.

    Example:
    Declare two variables and assign to them a RegExp object:

         auto r = RegExp("pattern");
         auto s = RegExp(r"p[1-5]\s*");

  RegExp search(string string);
  int opApply(scope int delegate(ref RegExp) dg);
    Set up for start of foreach loop.

    Returns:
    search() returns instance of RegExp set up to search string[].

    Example:

         import std.stdio;
         import std.regexp;

         void main()
         {
             foreach(m; RegExp("ab").search("abcabcabab"))
             {
                 writefln("%s[%s]%s", m.pre, m[0], m.post);
             }
         }
         // Prints:
         // [ab]cabcabab
         // abc[ab]cabab
         // abcabc[ab]ab
         // abcabcab[ab]

  string opIndex(size_t n);
    Retrieve match n.

    n==0 means the matched substring, n>0 means the n'th parenthesized subexpression. if n is larger than the number of parenthesized subexpressions, null is returned.

  string match(size_t n);
    Same as opIndex(n).

    WARNING:
    Scheduled for deprecation due to confusion with overloaded match(string). Instead of regex.match(n) you may want to use regex[n].

  @property string pre();
    Return the slice of the input that precedes the matched substring.

  @property string post();
    Return the slice of the input that follows the matched substring.

  string[] split(string s);
    Split s[] into an array of strings, using the regular expression as the separator.

    Returns:
    array of slices into s[]

  sizediff_t find(string string);
    Search string[] for match with regular expression.

    Returns:
    index of match if successful, -1 if not found

  string[] match(string s);
    Search s[] for match.

    Returns:
    If global attribute, return same value as exec(s). If not global attribute, return array of all matches.

  string replace(string s, string format);
    Find regular expression matches in s[]. Replace those matches with a new string composed of format[] merged with the result of the matches. If global, replace all matches. Otherwise, replace first match.

    Returns:
    the new string

  string[] exec(string s);
    Search string[] for match.

    Returns:
    array of slices into string[] representing matches

  string[] exec();
    Pick up where last exec(string) or exec() left off, searching string[] for next match.

    Returns:
    array of slices into string[] representing matches

  bool test(string s);
    Search s[] for match.

    Returns:
    0 for no match, !=0 for match

    Example:

        import std.stdio;
        import std.regexp;
        import std.string;

        int grep(int delegate(char[]) pred, char[][] list)
        {
          int count;
          foreach (s; list)
          {  if (pred(s))
               ++count;
          }
          return count;
        }

        void main()
        {
          auto x = grep(&RegExp("[Ff]oo").test,
                        std.string.split("mary had a foo lamb"));
          writefln(x);
        }

    which prints: 1

  int test();
    Pick up where last test(string) or test() left off, and search again.

    Returns:
    0 for no match, !=0 for match

  int test(string s, size_t startindex);
    Test s[] starting at startindex against regular expression.

    Returns:
    0 for no match, !=0 for match

  string replace(string format);
    After a match is found with test(), this function will take the match results and, using the format string, generate and return a new string.

  string replaceOld(string format);
    Like replace(char[] format), but uses old style formatting:

    Format | Description
    --- | ---
    & | replace with the match
    \n | replace with the nth parenthesized match, n is 1..9
    \c | replace with char c.

Copyright Digital Mars 2000 - 2011. | Page generated by Ddoc.