Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Non-capturing group reg-ex in Sublime Text not working

enter image description here I'm trying to remove all lingering spaces between tags. So I try to select them with a regex.

<span>        </span>
      ^^^^^^^^

My regex is (?:>) +(?:<). I'm trying to exclude the > and < from the selection with a non-capturing group, but it doesn't seem to be working.

At the moment, these two regexes seem to do the exact same thing:

With non-capturing groups: (?:>) +(?:<)

Without non-capturing groups: > +<

I think my understanding of regex is not good enough, but I'm not sure. What's wrong here?

like image 475
Azeirah Avatar asked Jun 25 '14 15:06

Azeirah


1 Answers

A non-capturing group doesn't capture the subpattern in a group (that you can refer later), however, all that have been matched in a non-capturing group is not excluded from the whole match result.

The way to solve the problem is to use lookarounds that are zero-width assertions. Lookarounds are only tests and are not part of the final result.

for spaces:

(?<=>) +(?=<)

for all whitespace-characters:

(?<=>)\s+(?=<)

(An other solution consists to use > +< with >< as replacement string)

like image 129
Casimir et Hippolyte Avatar answered Dec 13 '22 20:12

Casimir et Hippolyte