Next: , Previous: , Up: irregex sre   [Index]


50.9.5 Assertion patterns

It can be useful to assert something about the area around a pattern, without explicitly making it part of the pattern. The most common cases are specifically anchoring some pattern to the beginning or end of a word or line or even the whole string. For example, to match on the end of a word:

(irregex-match '(: "foo" eow) "foo")
⇒ #<match>

(irregex-match '(: "foo" eow) "foo!")
⇒ #<match>

(irregex-match '(: "foo" eow) "foof")
⇒ #f

The bow, bol, eol, bos and eos work similarly. nwb asserts that you are not in a word–boundary; if replaced with eow in the above examples it would reverse all the results.

There is no wb, since we probably know from context whether it would be the beginning or end of a word, but if we need it we can always use (or bow eow).

Somewhat more generally, Perl introduced positive and negative look-ahead and look-behind patterns. Perl’s look-behind patterns are limited to a fixed length, however the (vicare irregex) versions have no such limit.

(irregex-match '(: "regular" (look-ahead " expression"))
               "regular expression")
⇒ #<match>

The most general case, of course, would be an and pattern to complement the or pattern; all the patterns must match or the whole pattern fails. This may be provided in a future release, although it (and look-ahead and look-behind assertions) are unlikely to be compiled efficiently.