Search code examples
regexsmalltalkpharo

How do I match [ in a Smalltalk regular expression?


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 [.


Solution

  • Unsupported

    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.

    Workaround

    A workaround would be to split the range set into or-ed group, e.g.

    [[a-z]
    

    into

    (\[|[a-z])
    

    Better Tool

    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.