4 ms·
TXR has actual regex intersection and complement. Match one or more digits, but not "3": $ txr This is the TXR Lisp interactive listener of TXR 172. Use
by kazinator 10y ago
TXR has actual regex intersection and complement.
Match one or more digits, but not "3":
$ txr
This is the TXR Lisp interactive listener of TXR 172.
Use the :quit command or type Ctrl-D on empty line to exit.
1> [#/\d+&~3/ "1"]
"1"
2> [#/\d+&~3/ "3"]
nil
3> [#/\d+&~3/ "123"]
"123"
4> [#/\d+&~3/ ""]
nil
This is most easily understood by equivalence between regexes and sets of strings: ~3 means all strings that are not 3. This set includes the "" string. It includes all strings of length 1 other than "3", and it includes all strings of length 2 and greater. It's simply the complement of the set { "3" }. ~R means that whatever set of strings R matches is complemented (w.r.t the universe set being the set of all possible strings).
Similarly A&B is just set intersection. Regular expression A denotes some set of strings, and B denotes some set of strings. A&B denotes the intersection of those sets.
Another example: search the string for the leftmost substring which starts with foo, ends with bar and does not contain abc, and is the longest possible such string at its given position:
1> [#/foo.*bar&~.*abc.*/ ""]
nil
2> [#/foo.*bar&~.*abc.*/ "foobar"]
"foobar"
3> [#/foo.*bar&~.*abc.*/ "foozzzbar"]
"foozzzbar"
4> [#/foo.*bar&~.*abc.*/ "foozabczzbar"]
nil
5> [#/foo.*bar&~.*abc.*/ "fooabcbar"]
nil
6> [#/foo.*bar&~.*abc.*/ "foobar1234"]
"foobar"
7> [#/foo.*bar&~.*abc.*/ "foobarabc"]
"foobar"
8> [#/foo.*bar&~.*abc.*/ "3foobarabc"]
"foobar"
9> [#/foo.*bar&~.*abc.*/ "3fooabcbarabc"]
nil
10> [#/foo.*bar&~.*abc.*/ "3fooabcbarabcfoobar"]
"foobar"