开发者

Regex issue - fine backwards and not forwards Textmate

开发者 https://www.devze.com 2023-02-28 17:07 出处:网络
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

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 开发者_如何学Cthe 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.


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


Sounds like perfectly normal behavior to me. You just need to use a reluctant quantifier, which means adding a ?, like so:

<comments>(?m:.*?)</comments>

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 use s to specify it. Those flavors tend to support an m/"multiline" mode as well, which changes the behavior of the ^ and $ anchors. In TextMate that's the default mode, and can't be changed.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号