例如,对于这个字符串,
div.img-wrapper img[title="Hello world"]
我想匹配第一个空格,但不匹配第二个空格(包含在 [] 中)。正则表达式是什么?
以下表达式将通过使用前瞻断言来完成这项工作。
_(?>[^[\]]*(\[|$))
下划线代表空格。该表达式不支持嵌套括号,因为正则表达式不足以表达嵌套的匹配结构。
_ Match the space and
(?> assert that it is not inside brackets
[^[\]]* by matching all characters except brackets
( followed by either
\[ an opening bracket (a space inside brackets
will have a closing bracket at this position)
| or
$ or no more characters (end of line).
)
)
UPDATE
这是另一个(更漂亮的)解决方案,使用否定前瞻断言。
_(?![^[\]]*])
它断言空格后面的下一个括号不是右括号。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)