regex
Access to regular expressions is obtained by loading the regex
library, using for example statements like
@library("reg:", "regex")
, @library("sub1", "regex", "replaceFirst")
, @library("sub1", "regex", "replaceAll")
.
The library exposes the following operators:
returns whether the input string matches the given pattern |
|
returns whether a substring of the input matches the given pattern |
|
replaces the first match of the pattern as specified |
|
replaces all matches of the pattern as specified |
|
removes the first match of the pattern |
|
removes all matches of the pattern |
matches
Returns whether the input string matches the given pattern.
matches(Input, Pattern[, Params])
Where:
-
Input
is the string to be searched. -
Pattern
is the pattern to be evaluated over the string (used to initialize ajava.util.regex.Pattern
object). -
Params
is an optional string argument containing parameters for the underlyingjava.util.regex.Pattern
object.
@library("reg:", "regex").
astring("hello world").
result(X) :- astring(Y), X = reg:matches(Y, "^hello").
@output("result").
result(#T)
contains
Returns whether a substring of the input matches the given pattern.
contains(Input, Pattern[, Params])
Where:
-
Input
is the string to be searched. -
Pattern
is the pattern to be evaluated over the string. -
Params
is an optional string argument containing parameters for the underlyingjava.util.regex.Pattern
object.
@library("reg:", "regex").
astring("hello world").
result(X) :- astring(Y), X = reg:contains(Y, "world").
@output("result").
result(#T)
replaceFirst
Replaces the first match of the pattern as specified.
replaceFirst(Input, Pattern, Replacement[, Params])
Where:
-
Input
is the string to be searched. -
Pattern
is the pattern to be evaluated over the string. -
Replacement
is the replacement string to be used. -
Params
is an optional string argument containing parameters for the underlyingjava.util.regex.Pattern
object.
@library("reg:", "regex").
astring("hello world").
result(X) :- astring(Y), X = reg:replaceFirst(Y, "world", "everyone").
@output("result").
result("hello everyone")
replaceAll
Replaces all matches of the pattern as specified.
replaceAll(Input, Pattern, Replacement[, Params])
Where:
-
Input
is the string to be searched. -
Pattern
is the pattern to be evaluated over the string. -
Replacement
is the replacement string to be used. -
Params
is an optional string argument containing parameters for the underlyingjava.util.regex.Pattern
object.
@library("reg:", "regex").
astring("hello world world").
result(X) :- astring(Y), X = reg:replaceAll(Y, "world", "everyone").
@output("result").
result("hello everyone everyone")
removeFirst
Removes the first match of the pattern.
removeFirst(Input, Pattern[, Params])
Where:
-
Input
is the string to be searched. -
Pattern
is the pattern to be evaluated over the string. -
Params
is an optional string argument containing parameters for the underlyingjava.util.regex.Pattern
object.
@library("reg:", "regex").
astring("hello world").
result(X) :- astring(Y), X = reg:removeFirst(Y, "world").
@output("result").
result("hello ")
removeAll
Removes all matches of the pattern.
removeAll(Input, Pattern[, Params])
Where:
-
Input
is the string to be searched. -
Pattern
is the pattern to be evaluated over the string. -
Params
is an optional string argument containing parameters for the underlyingjava.util.regex.Pattern
object.
@library("reg:", "regex").
astring("hello world world").
result(X) :- astring(Y), X = reg:removeAll(Y, "world").
@output("result").
result("hello ")
Below is an example of using regular expressions in rules.
@library("reg:", "regex"). % The alias 'reg:' can be used to access both 'replaceFirst' and 'replaceAll'
@library("sub", "regex", "replaceAll"). % The alias 'sub' refers to replaceAll
pattern("^\([a-z]*\)").
input("(AB) (LINE1)\n(cd) (LINE2)\n(EF) (LINE3)").
res1(Y) :- input(X), pattern(P), Y = sub(X, P, "*****").
res2(Y) :- input(X), pattern(P), Y = sub(X, P, "*****", "MULTILINE | CASE_INSENSITIVE").
res3(Y) :- input(X), pattern(P), Y = reg:replaceFirst(X, P, "*****", "CASE_INSENSITIVE").
res4(Y) :- input(X), pattern(P), Y = reg:replaceAll(X, P, "*****", "MULTILINE").
@output("res1").
@output("res2").
@output("res3").
@output("res4").
res1("(AB) (LINE1)\n(cd) (LINE2)\n(EF) (LINE3)")
res2("***** (LINE1)\n***** (LINE2)\n***** (LINE3)")
res3("***** (LINE1)\n(cd) (LINE2)\n(EF) (LINE3)")
res4("(AB) (LINE1)\n***** (LINE2)\n(EF) (LINE3)")