Kenny Shen
Kenny Shen

Reputation: 4883

Regex issue - fine backwards and not forwards Textmate

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.

Upvotes: 0

Views: 143

Answers (2)

Alan Moore
Alan Moore

Reputation: 75242

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.

Upvotes: 0

drysdam
drysdam

Reputation: 8637

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

Upvotes: 2

Related Questions