繁体   English   中英

正则表达式最短匹配

[英]Regex shortest possible match

鉴于:

A 1234 AAAAAA AAAAAA 1234 7th XXXXX Rd XXXXXX

我要搭配:

1234 7th XXXXX Rd

仅使用Rd\\d+所以我尝试了: \\d+.*?Rd

但是我认为它从第一个1234开始一直到Rd匹配,而不是第二个1234 .*? 将匹配最短的匹配,我在做什么错?

使用以下模式:

^.*(1234 7th.*?Rd).*$

说明:

^.*        from the start of the greedily consume everything until
(1234 7th  capture from the last occurrence of 1234 7th
.*?Rd)     then non greedily consume everything until the first Rd
.*$        consume, but don't capture, the remainder of the string

这是一个代码片段:

 var input = "A 1234 AAAAAA AAAAAA 1234 7th XXXXX Rd XXXXXX"; var regex = /^.*(1234 7th.*?Rd).*$/g; var match = regex.exec(input); console.log(match[1]); // 1234 7th XXXXX Rd 

添加.*会匹配任何内容时,您使用的不仅仅是Rd\\d+ 如果可以将NUMBER-SPACE-SOMETHING-Rd假定为格式-那么可以将\\s添加到混合中并使用

/(\d+\s+\d+.*?Rd)/

 console.log('A 1234 AAAAAA AAAAAA 1234 7th XXXXX Rd XXXXXX'.match(/(\\d+\\s+\\d+.*?Rd)/g)) 

暂无
暂无

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

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