Regex - only matching wildcard of a certain length or less

点点圈 提交于 2019-11-28 06:34:02

问题


Is there a way to have a Regex statement search for a wildcard with a maximum length? For instance:

somestuff.*morestuff

If I wanted the above to match

somestuffblahmorestuff

but not

somestuffblahblahmorestuff

Is this possible?


回答1:


To match a known length use .{2,5} where the 2 is the minimum number of characters and 5 is the max. both values are optional but you do need one or the other

More can be read on this topic here




回答2:


in regex:

{n} Matches the previous element exactly n times.

{n,} Matches the previous element at least n times.

{n,m} Matches the previous element at least n times, but no more than m times.

for example:

,\d{3} matches ,876, ,543, and ,210 in 9,876,543,210

\d{2,} matches 166, 29, 1930

\d{3,5} matches 19302 in 193024




回答3:


somestuff.{4,7}morestuff

{min, max} is the syntax to specify the number of repetition.



来源:https://stackoverflow.com/questions/17120249/regex-only-matching-wildcard-of-a-certain-length-or-less

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