:- use_module(library(regex)).
?- '99 Bottles of Beer' =~ '[0-9]+ bottles'/i.
true.
Regular expression support for Prolog.
When Prologers want to match a string against a pattern, they typically write a DCG. DCGs are powerful and flexible. For medium to large patterns, they are also easier to read and maintain. However, for small and local patterns the overhead of writing and naming auxiliary predicates can be too much. In those circumstances, one might prefer a regular expression. This pack makes it possible.
The =~
operator matches a string (on the left side) against a regular
expression (on the right side). Either side can be an atom or a list of codes.
The \~
operator succeeds if the string does not match the pattern.
This section lists the regular expression syntax accepted by library(regex). Syntax not listed here is not yet supported. Patches welcome.
.
- any character, including newline[xyz]
- character class[^xyz]
- negated character class\d
- Perl character class\D
- negated Perl character class
xy
-x
followed byy
x|y
-x
ory
(prefer x)
x*
- zero or morex
, prefer morex+
- one or morex
, prefer morex?
- zero or onex
, prefer onex{n,m}
-n
orn+1
or ... orm
x
, prefer morex{n,}
-n
or morex
, prefer morex{n}
- exactlyn
x
(re)
- numbered capturing group(?<name>re)
- named & numbered capturing group
i
- case-insensitive (default false)s
- let.
match\n
(default false)
^
- at start of text$
- at end of text
x
- single characterA-Z
- character range (inclusive)
\d
- digits (same as[0-9]
)\D
- not digits (same as[^0-9]
)\s
- whitespace (same as[\t\n\f\r ]
)\S
- not whitespace (same as[^\t\n\f\r ]
)\w
- word characters (same as[0-9A-Za-z_]
)\W
- not word characters (same as[^0-9A-Za-z_]
)
Rob Cameron for his lecture notes on which the original implementation was based.
Using SWI-Prolog 6.3 or later:
?- pack_install(regex).
This module uses semantic versioning.
Source code available and pull requests accepted at https://github.com/mndrix/regex