问题
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?
回答1:
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)
回答2:
The purpose of non-capturing groups is to allow you to interact with a set of characters as a group without making it a submatch that you can use in a reference. So you are correct (?:>) +(?:<) is equivalent for your purposes to > +<.
来源:https://stackoverflow.com/questions/24412844/non-capturing-group-reg-ex-in-sublime-text-not-working