https://github.com/dnmfarrell/wildcard
Prolog DCG to match string patterns
https://github.com/dnmfarrell/wildcard
Last synced: 3 months ago
JSON representation
Prolog DCG to match string patterns
- Host: GitHub
- URL: https://github.com/dnmfarrell/wildcard
- Owner: dnmfarrell
- License: mit
- Created: 2024-10-16T21:45:03.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2025-01-31T22:01:32.000Z (over 1 year ago)
- Last Synced: 2025-03-06T05:43:32.437Z (over 1 year ago)
- Language: Prolog
- Size: 7.81 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# wildcard
Prolog DCG to match string patterns. Supports:
- `*` wildcard: matches any pattern including the empty string.
- `?` placeholder: matches any single atom.
- `\` escape: turns `*`, `?` and `\` into literal tokens.
N.B. this module intentionally does not verify its arguments are strings - additional clauses can be added to restrict the generated characters (examples below).
Requires [Scryer-Prolog](https://www.scryer.pl/) or similar interpreter.
## Usage
Exports `patt//1` which matches a string pattern to a string:
$ scryer-prolog src/wildcard.pl
?- phrase(patt("1?3"), "123").
true.
?- phrase(patt("*"), "123").
true.
?- phrase(patt("***"), "123").
true.
?- phrase(patt("???"), "123").
true.
?- phrase(patt("?*?"), "123").
true.
Wildcard can also generate strings for given pattern, with a helper predicate to restrict the generated characters:
?- [user].
uc_char(Char) :- member(Char, "ABCDEFGHIJKLMNOPQRSTUVWXYZ").
?- phrase(patt("D?G"), Cs), maplist(uc_char, Cs).
Cs = "DAG"
; Cs = "DBG"
; Cs = "DCG"
; Cs = "DDG"
; ... .
Limit the search space for wildcard patterns by length:
?- length(Cs, 4), phrase(patt("D*G"), Cs), maplist(uc_char, Cs).
Cs = "DAAG"
; Cs = "DABG"
; Cs = "DACG"
; Cs = "DADG"
; Cs = "DAEG"
; ... .
## Testing
$ scryer-prolog -f test/wildcard.pl
Running test "a-ab"
Running test "A-a"
Running test "aB-ab"
Running test "abC-abc"
Running test "a-a"
Running test "ab-ab"
Running test "?a-a"
Running test "a?-a"
Running test "?-a"
Running test "a?-ab"
Running test "a?c-abc"
Running test "???-abc"
Running test "*a-a"
Running test "a*-a"
Running test "*-a"
Running test "a*-ab"
Running test "a*c-abc"
Running test "*-abc"
Running test "***-abc"
Running test "*?-a"
Running test "*?*-a"
Running test "?*?-ab"
Running test "?*?-abc"
Running test "?*-abcde"
Running test "?*-a"
Running test "\?-?"
Running test "\?\?-??"
Running test "\*-*"
Running test "\*\*-**"
Running test "\A-A"
Running test "\A-\A"