Skip to end of metadata
Go to start of metadata


Boo has built-in support for regular expression literals.


You surround the regular expression with / /,
  or @/ / for more complex expressions that contain whitespace.
 Boo even has an =~ operator like Perl.

Here are some samples of using regular expressions in boo:



Specifying regex options

One limitation of using built-in support for regular expressions is that you can't use non-standard regex options like regex compiled.

Actually there is a way to use the ignore case option. Add a (?i) to the beginnning of your regex pattern like so:



But in other cases you may want to just use the .NET Regex class explicitly:



Regex Replace method

This would be an equivalent to using perl's switch/replace statement: s/foo/bar/g.



regex primitive type

Also note, Boo has a built-in primitive type called "regex" (lowercase) that means the same thing as the .NET Regex class. So you can do for example:



See also:

  • No labels

1 Comment

  1. Hope this is usefull for someone.
    Suppose you want all the substrings in a text which match a certain regular expression. I use 2 different solutions:

    import System.Text.RegularExpressions
    TheSea="Fisherman has gone fishing. How many fishes will he catch?"

    def GrabAll( SearchString as string, re as Regex):
    m=re.Match( SearchString)
    while m.Success:
    yield m.Groups
    m=m.NextMatch()

    // the simplest & easiest way (without any help function)
    for m as Match in @/Ffish.+/.Matches( TheSea):
    print m.Value

    // a more powerful technique, using named groups
    Net=Regex("(?<Fishy>fish)(?<rest>.+)", RegexOptions.IgnoreCase)
    for tag in GrabAll( TheSea, Net):
    print tag"Fishy".Value, tag"rest".Value

    /* Output:
    Fisherman
    fishing
    fishes
    Fish erman
    fish ing
    fish es
    */