简体   繁体   中英

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

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, eg

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

I am adding a GNU Smalltalk related answer because the question is tagged with [smalltalk] and therefore likely to turn up in internet search results.

In GNU Smalltalk, regexs have Perl like synta x, and the character [ can be escaped as \\[ . For example:

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

Escaping works within a range as well:

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

Which probably makes it worth while to mention that the message =~ can be passed to a string along with a regex.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM