Expression Pattern Language (eXPL)

eXPL Regular Expressions

Regular expressions match text to a pattern and optionally collect portions of text referred to as groups. eXPL regular expressions are implemented using the Java Pattern class.

Format

A regular expression declaration has format:

regex(regular-expression [ { group-name [ , group-name ... ] } ] ).

This declaration is placed immediately following the term to be matched and the regex short circuits unification if the match fails. Note that the regular expression may be contained in a string variable so it does not clutter the declaration. The optional groups are implemented as terms which are inserted in the template following the match term.

Here is an example of a term named "Definition" which matches to a single text string an collects 2 group items named "part" (for "part of speech") and "def" for "definition". Sample Definition: "n. a positive motivational influence":

Definition regex("^(.)\\. (.*+)" { part, def })

Regex Groups Example

The following script builds on the regex example given in the Expressions section. In the original example, just one regex is used to pick out "in" words from a dictionary. This script adds a second regex with 2 groups, one is part of speech and the other is the definition of the word. The word is changed to a dictionary entry with part of speech expanded from a letter to a word for readability.

axiom lexicon (Word, Definition) : resource "lexicon";
string wordRegex = "^in[^ ]+";
string defRegex = "^(.)\\. (.*+)";
// Convert single letter part of speech to word
axiom expand =
  { n = "noun",
    v = "verb",
    a = "adv.",
    j = "adj." };
// Collect words starting with 'in' along with other details
axiom word_definitions = {};
calc in_words (
  word regex(wordRegex),
  definition regex(defRegex { part, def }),
  axiom in_word = { word + ", " + expand[part] +
    "- " + def },
  word_definitions += in_word
);
query query_in_words(lexicon : in_words)

The tutorial11 RegexGroups code example runs this query. Here is the first solution listed when the query is run:

inadequate, adj.- not sufficient to meet a need
incentive, noun- a positive motivational influence
incidence, noun- the relative frequency of occurrence...