[英]Single regular expression for two different strings
I need to write a single regular expression that returns the color value and size values from the below two strings. 我需要编写一个正则表达式,从下面两个字符串返回颜色值和大小值。
[{"id":"2","name":"Color","code":"COLOR","optionValue":{"value":"TANGERINE TANGO","priority":0,"altValue1":"ORANGE","altValue2":null}},{"id":"3","name":"Size","code":"SIZE","optionValue":{"value":"MEDIUM","priority":4,"altValue1":null,"altValue2":null}}]
[{"id":"3","name":"Size","code":"SIZE","optionValue":{"value":"MEDIUM","priority":4,"altValue1":null,"altValue2":null}},{"id":"2","name":"Color","code":"COLOR","optionValue":{"value":"PEACOCK BLUE","priority":0,"altValue1":"GREEN","altValue2":null}}]
Currently I have two different regexps for them respectively. 目前,我分别为他们有两个不同的正则表达式。
1) COLOR(?:.*?)value":"([^"]+)(?:.*?)SIZE(?:.*?)value":"([^"]+)"
2) SIZE(?:.*?)value":"([^"]+)(?:.*?)COLOR(?:.*?)value":"([^"]+)"
Is there a way I can achieve this using a single regex? 有没有一种方法可以使用单个正则表达式来实现?
Use JSON.parse , it is safer and is more appropriate with JSON strings: 使用JSON.parse ,它更安全,更适合JSON字符串:
var strings = ['[{"id":"2","name":"Color","code":"COLOR","optionValue":{"value":"TANGERINE TANGO","priority":0,"altValue1":"ORANGE","altValue2":null}},{"id":"3","name":"Size","code":"SIZE","optionValue":{"value":"MEDIUM","priority":4,"altValue1":null,"altValue2":null}}]', '[{"id":"3","name":"Size","code":"SIZE","optionValue":{"value":"MEDIUM","priority":4,"altValue1":null,"altValue2":null}},{"id":"2","name":"Color","code":"COLOR","optionValue":{"value":"PEACOCK BLUE","priority":0,"altValue1":"GREEN","altValue2":null}}]']; var cnt = 0; strings.forEach(function(str) { var array = JSON.parse(str); cnt += 1; document.getElementById("r").innerHTML += "<b>Match " + cnt + "</b><br/>"; array.forEach(function(object) { document.getElementById("r").innerHTML += object.optionValue.value + "<br/>"; }); });
<div id="r"/>
You can declare an array and push
the results you get into the array for later use. 您可以声明一个数组,然后
push
获得的结果push
送到数组中以供以后使用。
(COLOR|SIZE).*?value":"([^"]+).*?(?!\1)(?:COLOR|SIZE).*?value":"([^"]+)"
I might agree with @stribizhev and @Sirko about parsing the JSON, but I can see if you just need to get a quick-and-dirty job done, then a regex is sometimes useful. 对于解析JSON,我可能同意@stribizhev和@Sirko,但是我可以看到是否只需要完成快速工作,那么正则表达式有时会很有用。
You can use alternation , capturing , lookahead assertions , and backreferencing . 您可以使用交替 , 捕获 , 超前断言和反向引用 。
First, let's simplify your regex by removing unnecessary non-capturing groups, (?:...)
: 首先,让我们通过删除不必要的非捕获组
(?:...)
简化正则表达式:
COLOR.*?value":"([^"]+).*?SIZE.*?value":"([^"]+)"
SIZE.*?value":"([^"]+).*?COLOR.*?value":"([^"]+)"
Now, here's what gets you halfway (similar to @MarcosPerezGude's suggestion): 现在,这是让您半途而废的方法(类似于@MarcosPerezGude的建议):
(?:COLOR|SIZE).*?value":"([^"]+).*?(?:COLOR|SIZE).*?value":"([^"]+)"
^^^ ^^^^^^ ^^^^^^^^^ ^
But the problem with this is it accepts COLOR
COLOR
and SIZE
SIZE
. 但这是一个问题,它接受
COLOR
COLOR
和SIZE
SIZE
。 Here's how to get around that: 解决方法如下:
( COLOR|SIZE).*?value":"([^"]+).*?(?!\1)(?:COLOR|SIZE).*?value":"([^"]+)"
^^ ^^^^^^
Let me explain this. 让我解释一下。 The
\\1
is a backreference to whatever's captured in the first capturing group. \\1
是对第一个捕获组中捕获的任何内容的反向引用 。 Which in our case is now COLOR
or SIZE
because we've removed the non-capturing-ness. 在我们的情况下,现在是
COLOR
或SIZE
因为我们已删除了非捕获性。 The (?!\\1)
is a negative lookahead assertion that says, "As long as what comes next isn't \\1
..." Therefore if the captured string was COLOR
, the second half must be SIZE
, or vice versa. (?!\\1)
是一个否定的超前断言 ,它表示:“只要接下来出现的不是\\1
...”,因此,如果捕获的字符串为COLOR
,则后半部分必须为SIZE
,反之亦然。
You can try with the OR operator 您可以尝试使用OR运算符
([size|color](?:.*?))
Good luck! 祝好运!
您应该能够让它在两个备选方案之间进行选择,如下所示:
COLOR(?:.*?)value":"([^"]+)(?:.*?)SIZE(?:.*?)value":"([^"]+)|SIZE(?:.*?)value":"([^"]+)(?:.*?)COLOR(?:.*?)value":"([^"]+)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.