正则表达式问题 - 向后精细而不是向前 Textmate
我不太擅长正则表达式,但在 Textmate 中,我试图清除 XML 文件中的一些多行,看起来像
<comments>
<sub_node>....
....
</comments>
我在查找/替换正则表达式时使用它,
<comments>(?m:.*)</comments>
有多次出现上面的内容,但如果我进行查找,它会找到第一个,然后选择中间的所有内容,包括外部节点,直到文件中的最后一个。
如果我从最后一行查找上一个(向后),它会正确捕获一个块。我不确定我在这里做错了什么,以及是否有人会建议一种更有效的方法来做到这一点。
谢谢。
I'm not very good with regular expressions but in Textmate, I'm trying to clear out some multi-lines in an XML file that looks like
<comments>
<sub_node>....
....
</comments>
and I'm using this in the find/replace with regex,
<comments>(?m:.*)</comments>
There're multiple occurences of the above, but if I do a find, it finds the first and then selects everything in between including outside nodes till the last in the file.
If I do a find previous (backwards) from the last line it captures a block correctly. I'm not sure what I'm doing wrong here and if anyone might even suggest a far more efficient way of doing this.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要使用非贪婪限定符。我对Textmate一无所知,所以我不知道它是否支持它们。如果没有,您可以搜索
,后跟任意数量的非内容,后跟
<评论>
。 (这将是更具体的帮助,但您发布的示例不熟悉,并且一定是一些 Textmate 怪异。)You need to use non-greedy qualifiers. I don't know anything about Textmate, so I don't know if it supports them. If it doesn't, you can search for
<comments>
followed by any number of things that isn't</comments>
followed by<comments>
. (This would be more specific help, but your posted example is unfamiliar and must be some Textmate weirdness.)对我来说听起来是完全正常的行为。您只需要使用一个不情愿的量词,这意味着添加一个
?
,如下所示:这里唯一奇怪的是
m
(表示“多行”)修饰符,它允许.
元字符来匹配换行符。大多数正则表达式风格称之为“单行”或“点匹配全部”模式,并使用s
来指定它。这些风格往往也支持m
/"multiline" 模式,这会改变^
和$
锚点。在 TextMate 中,这是默认模式,无法更改。Sounds like perfectly normal behavior to me. You just need to use a reluctant quantifier, which means adding a
?
, like so:The only weirdness here is the
m
(for "multiline") modifier, which allows the.
metacharacter to match newlines. Most regex flavors call that "single-line" or "dot-matches-all" mode, and uses
to specify it. Those flavors tend to support anm
/"multiline" mode as well, which changes the behavior of the^
and$
anchors. In TextMate that's the default mode, and can't be changed.