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

十年热恋 提交于 2019-12-22 05:50:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!