0

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.

davidcondrey
  • 34,416
  • 17
  • 114
  • 136
Kenny Shen
  • 4,773
  • 3
  • 21
  • 18

2 Answers2

2

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

drysdam
  • 8,341
  • 1
  • 20
  • 23
0

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.

Alan Moore
  • 73,866
  • 12
  • 100
  • 156