Re:The New ‘Absent Operator’ in Ruby’s Regular Expressions
by kazinator on Thursday March 23, 2017 @03:31PM
(#13943335)
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:
Match one or more digits, but not "3":
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: