Did you know ... Search Documentation:
Packs (add-ons) for SWI-Prolog

Package "regex"

Title:Regular expressions
Rating:Not rated. Create the first rating!
Latest version:0.3.3
SHA1 sum:7f6049bbbc6e2b22f2f3d69eea515a874335bec1
Author:Michael Hendricks <michael@ndrix.org>
Maintainer:Michael Hendricks <michael@ndrix.org>
Packager:Michael Hendricks <michael@ndrix.org>
Home page:https://github.com/mndrix/regex
Download URL:https://github.com/mndrix/regex/archive/v0.3.3.zip

Reviews

No reviews. Create the first review!.

Details by download location

VersionSHA1#DownloadsURL
0.0.1a423c219733d75b0875c89027a0f9980de4004661http://packs.ndrix.com/regex/regex-0.0.1.tgz
0.0.377b90f7eff72af8a87814da7e4d99af2fe2c72332http://packs.ndrix.com/regex/regex-0.0.3.tgz
0.0.4091abcd2ddcba491c108b620b24e200c25b732513http://packs.ndrix.com/regex/regex-0.0.4.tgz
0.1.0a9d4013deb806748a542d99405bb14f8c3a04a192http://packs.ndrix.com/regex/regex-0.1.0.tgz
0.2.0eb028c62593fbda1d013aab8b33bdf59d61824152http://packs.ndrix.com/regex/regex-0.2.0.tgz
0.2.16317df47b22bc9369ee8a91134c30821bc36c2ae18http://packs.ndrix.com/regex/regex-0.2.1.tgz
0.2.30b821e04f3068a53b9117a24c33e3f4ea628457211http://packs.ndrix.com/regex/regex-0.2.3.tgz
0.2.42f63d602898a77c44ff25c9de8fc14ee5c3a4699146http://packs.ndrix.com/regex/regex-0.2.4.tgz
0.3.0079de5e766aca63f86e2b422d0c8e640765627dc1https://github.com/mndrix/regex/archive/v0.3.0.zip
0.3.1b433612e436de30ee36204792e67420215c76b1a119https://github.com/mndrix/regex/archive/v0.3.1.zip
0.3.285750903b8124aa39d6a528649b4df5ddce2e70f90https://github.com/mndrix/regex/archive/v0.3.2.zip
http://storage.googleapis.com/packs.ndrix.com/regex-0.3.2.zip
0.3.37f6049bbbc6e2b22f2f3d69eea515a874335bec128http://github.com/mndrix/regex/archive/v0.3.3.zip

Synopsis

:- use_module(library(regex)).
?- '99 Bottles of Beer' =~ '[0-9]+ bottles'/i.
true.

Description

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.

Syntax Reference

This section lists the regular expression syntax accepted by library(regex). Syntax not listed here is not yet supported. Patches welcome.

Single characters

  • `.` - any character, including newline
  • [xyz] - character class
  • `[^xyz]` - negated character class
  • \d - Perl character class
  • \D - negated Perl character class

Composites

  • xy - x followed by y
  • x|y - x or y (prefer x)

Repetitions

  • `x*` - zero or more x, prefer more
  • `x+` - one or more x, prefer more
  • `x?` - zero or one x, prefer one
  • `x{n,m} - n or n+1` or ... or m x, prefer more
  • `x{n,} - n` or more x, prefer more
  • `x{n} - exactly n x`

Grouping

  • (re) - numbered capturing group
  • `(?<name>re)` - named & numbered capturing group

Flags

  • i - case-insensitive (default false)
  • s - let `. match \n` (default false)

Empty strings

  • ^ - at start of text
  • $ - at end of text

Character class elements

  • x - single character
  • A-Z - character range (inclusive)

Perl character classes

  • \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_]`)

Acknowledgements

Rob Cameron for his lecture notes on which the original implementation was based.

Installation

Using SWI-Prolog 6.3 or later:

?- pack_install(regex).

This module uses semantic versioning.

Source code available and pull requests accepted at http://github.com/mndrix/regex

Contents of pack "regex"

Pack contains 14 files holding a total of 25.1K bytes.