简体   繁体   English

正则表达式 - 仅匹配一定长度或更短的通配符

[英]Regex - only matching wildcard of a certain length or less

Is there a way to have a Regex statement search for a wildcard with a maximum length?有没有办法让 Regex 语句搜索具有最大长度的通配符? For instance:例如:

somestuff.*morestuff

If I wanted the above to match如果我希望以上匹配

somestuffblahmorestuff

but not但不是

somestuffblahblahmorestuff

Is this possible?这可能吗?

To match a known length use .{2,5} where the 2 is the minimum number of characters and 5 is the max.要匹配已知长度,请使用.{2,5} ,其中 2 是最小字符数,5 是最大值。 both values are optional but you do need one or the other这两个值都是可选的,但你确实需要一个或另一个

More can be read on this topic here可以在此处阅读有关此主题的更多信息

In regex:在正则表达式中:

{n}
Matches the previous element exactly n times.精确匹配前一个元素n次。

{n,}
Matches the previous element at least n times.至少匹配前一个元素n次。

{n,m}
Matches the previous element at least n times, but no more than m times.匹配前一个元素至少n次,但不超过m次。

For example:例如:

,\d{3} matches ,876 , ,543 , and ,210 in 9,876,543,210 ,\d{3}匹配9,876,543,210中的,876 , ,543,210

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

\d{3,5} matches 19302 in 193024 \d{3,5}匹配19302中的193024

somestuff.{4,7}morestuff

{min, max} is the syntax to specify the number of repetition. {min, max} 是指定重复次数的语法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM