简体   繁体   English

正则表达式匹配不以特定字符串结尾的组

[英]Regex match a group not ending with specific string

I'm using javascript regex engine, and I have difficulties using a regex : 我正在使用javascript正则表达式引擎,并且在使用正则表达式时遇到了困难:

Here is a Sample (it's an ugly js script extraction of what I have to match): 这是一个示例(这是我必须匹配的丑陋的js脚本提取):

{data = function() {[{id:"1441-aeze",name:"theme1"},{id:"1441-aezb",name:"theme2"},{id:"1441-aezc",name:"theme3-a"},{id:"1441-aezd",name:"theme1-mtr"},{id:"1441-aezf",name:"theme4"}]}}

My regex try : 我的正则表达式尝试:

id:"([^"]+)",name:"(?![^"]*mtr$)[^"]*"

Expected result for matching group 1: 匹配组1的预期结果:

Match[1] : 1441-aeze
Match[1] : 1441-aezb
Match[1] : 1441-aezc
Match[1] : 1441-aezf

But it's matching every portion of my sample. 但这与我样本的每个部分都匹配。 I don't understand why 我不明白为什么

Here is reg101 : Regex101 example 这是reg101: Regex101示例

here is one way using this pattern 这是使用这种模式的一种方法

id:"([^"]+)",name:"(?:.(?!mtr))*?\}

Demo 演示


here is another 这是另一个

id:"([^"]+)",name:"(?:[^m]|m(?!tr))*?\}

Demo 演示

I'm not sure, what the $ in your lookahead is supposed to do, but unescaped it matches at the end of the string (or end of the line with m-Modifier) - but there is no end of a line at the end of your match. 我不确定,您的预想中的$应该做什么,但是在字符串的末尾(或带有m-Modifier的行的末尾)未转义的$匹配-但末尾没有行的末尾你的比赛。 In your pattern, the " signals the end of the match, so you can just replace the $ with " to get it working, like id:"([^"]+)",name:"(?![^"]*mtr")[^"]*" 在您的模式中, "表示比赛结束,因此您只需将$替换$ "即可使其生效,例如id:"([^"]+)",name:"(?![^"]*mtr")[^"]*"

See your updated link: https://regex101.com/r/tY1aG7/4 查看更新的链接: https : //regex101.com/r/tY1aG7/4

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

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