[英]How do I match [ in a Smalltalk regular expression?
I want to match [
in a regular expression in Pharo 6. 我希望匹配[
在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. 查看RxParser>>atom
和RxParser>>characterSet
,根本不支持在范围集中转义字符。
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, eg 解决方法是将范围集拆分为or-ed组,例如
[[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. 请注意,Pharo用户通常被指示使用PetitParser而不是正则表达式进行文本解析,因为PetitParser更易于管理和调试。 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. 我正在添加一个与GNU Smalltalk相关的答案,因为该问题用[smalltalk]标记,因此可能会出现在互联网搜索结果中。
In GNU Smalltalk, regexs have Perl like synta x, and the character [
can be escaped as \\[
. 在GNU Smalltalk中,正则表达式具有类似于synta x的Perl ,并且字符[
可以转义为\\[
。 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. 这可能值得一提,message =~
可以与正则表达式一起传递给字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.