I want to match [
in a regular expression in Pharo 6.
This works fine:
| matcher |
matcher := RxMatcher forString: '\['.
matcher matches: '['. "produces true"
However, I can't see how to do this inside a []
. Neither [[]
nor [\[]
work.
I can match closing ]
fine with []]
, but I can't work out how to do this with [
.
Looking at the implementation of RxParser>>atom
and RxParser>>characterSet
, escaping characters in the range set is simply not supported.
According to the documentation, other "special" characters (^,-,]) can be handled only by a specific placement within the set so not to trigger parsing of a different branch.
A workaround would be to split the range set into or-ed group, e.g.
[[a-z]
into
(\[|[a-z])
Note that Pharo users are typically directed to use PetitParser instead of regular expressions for text parsing, as PetitParser is easier to manage and debug. A sort of more object-oriented take on regular expressions to say the least.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With