简体   繁体   English

正则表达式最短匹配

[英]Regex shortest possible match

Given: 鉴于:

A 1234 AAAAAA AAAAAA 1234 7th XXXXX Rd XXXXXX

I want to match: 我要搭配:

1234 7th XXXXX Rd

Using nothing more than Rd and \\d+ so i tried: \\d+.*?Rd 仅使用Rd\\d+所以我尝试了: \\d+.*?Rd

but it matches starting from the first 1234 up to Rd instead of the second 1234, i thought .*? 但是我认为它从第一个1234开始一直到Rd匹配,而不是第二个1234 .*? would match the shortest possible match, what am i doing wrong? 将匹配最短的匹配,我在做什么错?

Use the following pattern: 使用以下模式:

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

Explanation: 说明:

^.*        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

Here is a code snippet: 这是一个代码片段:

 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 

You are using more than Rd and \\d+ when you add .* which will match anything. 添加.*会匹配任何内容时,您使用的不仅仅是Rd\\d+ If you can assume NUMBER-SPACE-SOMETHING-Rd as the format - then you could add \\s to the mix and use 如果可以将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