简体   繁体   English

正则表达式-仅当组之间有指定的字符串时,才能从字符到字符匹配

[英]Regex - Match from Character to Character only if a specified String is in between the Group

I want to match the string between two ")" characters only if a specified word is found in the string. 我只想在字符串中找到指定的单词时才匹配两个“)”字符之间的字符串。

For example, consider this block of text: 例如,考虑以下文本块:

...Milwaukee 2 (FINAL) Arizona 6   Chicago Cubs 5 (BOT 6TH) NY Mets 4   Colorado 5 (FINAL) Detroit...

with the specified word being "Colorado". 指定的单词是“科罗拉多”。 The language being used is Kustom, but for simplicity, consider the language to be javascript with lookbehinds. 所使用的语言是Kustom,但为简单起见,请将该语言视为带有lookbehinds的javascript。

I would like to match: 我想搭配:

 NY Mets 4   Colorado 5 (FINAL)

I tried: (?<=\\)).*?(Colorado).*?(?:\\)) but it returns a match starting from the first ")" to the one after Colorado. 我试过: (?<=\\)).*?(Colorado).*?(?:\\))但它返回的匹配项是从第一个“)”到科罗拉多州之后的那个。

Staring from a word boundary this captures: 从单词边界开始,它捕获:

  • the word characters and spaces previous to the target word Colorado 目标单词Colorado之前的单词字符和空格
  • the target word Colorado 目标词科罗拉多
  • all subsequent word characters, spaces and opening brackets 所有后续的文字字符,空格和方括号
  • the closing bracket 结束括号

     \\b[\\w\\s]*?Colorado[\\w\\s\\(]*?\\) 

try this (?!\\s).*?Colorado.*?(?:\\)) 试试这个(?!\\s).*?Colorado.*?(?:\\))

 s = `...Milwaukee 2 (FINAL) Arizona 6 Chicago Cubs 5 (BOT 6TH) NY Mets 4 Colorado 5 (FINAL) Detroit...` m = s.match(/(?!\\s).*?Colorado.*?(?:\\))/gm) console.log(m[0]); 

` `

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

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