regex match number after (

安稳与你 提交于 2020-01-15 03:03:10

问题


I am trying to use regex to match a a variable length of digits after the open bracket '(' character.

I have tried

\(\d+

But that regex includes the bracket in the match. How do I exclude it?

I am using sublime text regex engine to do the match.


回答1:


You may use a positive lookbehind:

(?<=\()\d+

There is one limitation here: you can only have a pattern of known width in the lookbehind. You may use (?<=\(|\s{5})\d+, but you cannot use (?<=\d:\s*)\d+.

You may use \K "match reset" operator to work around the length limit in the lookbehind since \K just "truncates" the match at the place where it is used, all text matched by the pattern to the left of it is omitted:

\(\K\d+

[

]

Note that \K is not actually a lookbehind equivalent since the text before it is consumed while lookbehinds do not consume text.




回答2:


You can use capturing groups:

/\((\d+)/

The non-escaped brackets are a regex matching group which you can extract from each match. If you have a search'n'replace system, the $1 is often used to access the groups.



来源:https://stackoverflow.com/questions/42119058/regex-match-number-after

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