根据我从 Pharo 正则表达式文档中收集到的信息,我可以定义一个正则表达式对象,例如:
re := '(foo|re)bar' asRegex
我可以通过以下方式将匹配的正则表达式替换为字符串:
re copy: 'foobar blah rebar' replacingMatchesWith: 'meh'
这将导致:“meh blah meh”。
到目前为止,一切都很好。但我想更换'bar'
并保留前缀。因此,我需要一个变量来处理捕获的括号:
re copy: 'foobar blah rebar' replacingMatchesWith: '%1meh'
我想要结果:'foomeh blah remeh'
。然而,这只是给了我:'%1meh blah %1meh'
。我也尝试过使用\1
, or \\1
, or $1
, or {1}
并得到文字字符串替换,e.g., '\1meh blah \1meh'
因此。
我可以在 GNU Smalltalk 中轻松地做到这一点:
'foobar blah rebar' replacingAllRegex: '(foo|re)bar' with: '%1meh'
但我在 Pharo 正则表达式文档中找不到任何地方告诉我如何在 Pharo 中执行此操作。我也用谷歌搜索了 Pharo 正则表达式,但没有找到任何东西。此功能是 RxMatcher 类或其他 Pharo 正则表达式类的一部分吗?