How can I combine [in a Smalltalk regex?

I want to match [ in regex in Pharo 6.

This works great:

 | matcher | matcher := RxMatcher forString: '\['. matcher matches: '['. "produces true" 

However, I do not see how to do this inside [] . Neither [[] nor [\[] work.

I can match the closure of ] fine with []] , but I cannot decide how to do it with [ .

+5
source share
2 answers

unsupported

If you look at the implementation of RxParser>>atom and RxParser>>characterSet , escaping characters in a range set is simply not supported.

According to the documentation, other β€œspecial” characters (^, -,]) can be processed only by a specific placement inside the set, so as not to start parsing another branch.

Bypass

A workaround would be to split the range given into a group or, for example,

 [[az] 

in

 (\[|[az]) 

Best tool

Note that Pharo users are usually redirected to use PetitParser instead of regular expressions to parse text, since PetitParser is easier to manage and debug. A more than object-oriented approach takes at least regular expressions.

+5
source

I am adding an answer related to GNU Smalltalk because the question is marked with [smalltalk] and therefore may appear in Internet search results.

In GNU Smalltalk, regular expressions have Perl as synta x, and the symbol [ can be escaped as \[ . For instance:

 st> '[ac' =~ '\[[ab]' MatchingRegexResults:'[a' st> '[bc' =~ '\[[ab]' MatchingRegexResults:'[b' 

Shielding also works within the range of:

 st> '[bc' =~ '[\[b]' MatchingRegexResults:'[' 

Which is probably worth mentioning that the message =~ can be passed to a string along with the regular expression.

+1
source

Source: https://habr.com/ru/post/1271116/


All Articles