next up previous contents index
Next: 1.7.2 Splitting Up: 1.7 Regular expressions Previous: 1.7 Regular expressions   Contents   Index

1.7.1 Matching

The following snippet iteratively searches for capitalized words:

`This is an Onyx string.'

{dup `[A-Z]\w+' <$g true> match}{
    0 submatch 1 sprint
} while

The above code generates the following output:

`This'
`Onyx'

The $g flag to the match operator says to start searching where the previous match ended, which is what makes the while loop possible. The submatch operator gets the substring of the input string that the regular expression most recently matched.

With a slight modification to the previous example, it is possible to get at the capital letters, rather than the entire capitalized words. This is achieved by using a set of capturing parentheses, and changing the argument to submatch :

`This is an Onyx string.'

{dup `([A-Z])\w+' <$g true> match}{
    1 submatch 1 sprint
} while

This generates the following output:

`T'
`O'

This is a trivial example of how capturing subpatterns can be used, but the possibilities are wide and varied.



Jason Evans 2005-03-16