[英]How to do a complex multiple if-then-else regex?
I need to do a complex if-then-else with five preferential options.我需要用五个优先选项做一个复杂的 if-then-else。 Suppose I first want to match abc
but if it's not matched then match ac
, then if it's not matched def
, then %#@
, then 1z;
假设我首先想匹配abc
但如果它不匹配则匹配ac
,然后如果它不匹配def
,然后%#@
,然后1z;
. .
Can I nest the if-thens or how else would it be accomplished?我可以嵌套 if-thens 或者它是如何完成的? I've never used if-thens before.我以前从未使用过 if-thens。
For instance, in the string 1z;%#@defarcabcaqcdef%#@1z;
例如,在字符串1z;%#@defarcabcaqcdef%#@1z;
I would like the output abc
.我想要输出abc
。
In the string 1z;%#@defarcabaqcdef%#@1z;
在字符串1z;%#@defarcabaqcdef%#@1z;
I would like the output arc
.我想要输出arc
。
In the string 1z;%#@defacabacdef%#@1z;
在字符串1z;%#@defacabacdef%#@1z;
I would like the output def
.我想要输出def
。
In the string 1z;#@deacabacdf%#@1z;
在字符串1z;#@deacabacdf%#@1z;
I would like the output %#@
.我想要输出%#@
。
In the string foo;%@dfaabaef#@1z;barbbbaarr3
I would like the output 1z;
在字符串foo;%@dfaabaef#@1z;barbbbaarr3
我想要输出1z;
. .
You need to force individual matching of each option and not put them together.您需要强制对每个选项进行单独匹配,而不是将它们放在一起。 Doing so as such: .*?(?:x|y|z)
will match the first occurrence where any of the options are matched.这样做: .*?(?:x|y|z)
将匹配任何选项匹配的第一次出现。 Using that regex against a string, ie abczx
will return z
because that's the first match it found.对字符串使用该正则表达式,即abczx
将返回z
因为这是它找到的第一个匹配项。 To force prioritization you need to combine the logic of .*?
要强制确定优先级,您需要结合.*?
and each option such that you get a regex resembling .*?x|.*?y|.*?z
.和每个选项,这样你就会得到一个类似于.*?x|.*?y|.*?z
的正则表达式。 It will try each option one by one until a match is found.它将一个一个地尝试每个选项,直到找到匹配项。 So if x
doesn't exist, it'll continue to the next option, etc.所以如果x
不存在,它会继续下一个选项,等等。
See regex in use here请参阅此处使用的正则表达式
(?m)^(?:.*?(?=abc)|.*?(?=a.c)|.*?(?=def)|.*?(?=%#@)|.*?(?=1z;))(.{3})
(?m)
Enables multiline mode so that ^
and $
match the start/end of each line (?m)
启用多行模式,以便^
和$
匹配每行的开始/结束(?:.*?(?=abc)|.*?(?=ac)|.*?(?=def)|.*?(?=%#@)|.*?(?=1z;))
Match either of the following options (?:.*?(?=abc)|.*?(?=ac)|.*?(?=def)|.*?(?=%#@)|.*?(?=1z;))
匹配以下任一选项
.*?(?=abc)
Match any character any number of times, but as few as possible, ensuring what follows is abc
literally .*?(?=abc)
匹配任意字符任意次数,但尽可能少,确保后面的字符是abc
.*?(?=ac)
Match any character any number of times, but as few as possible, ensuring what follows is a
, any character, then c
.*?(?=ac)
匹配任意字符任意次数,但尽可能少,确保后面是a
,任意字符,然后是c
.*?(?=def)
Match any character any number of times, but as few as possible, ensuring what follows is def
literally .*?(?=def)
匹配任意字符任意次数,但尽可能少,确保后面的内容是def
字面意思.*?(?=%#@)
Match any character any number of times, but as few as possible, ensuring what follows is %#@
literally .*?(?=%#@)
匹配任意字符任意次数,但尽可能少,确保后面的字符是%#@
.*?(?=1z;)
Match any character any number of times, but as few as possible, ensuring what follows is 1z;
.*?(?=1z;)
匹配任意字符任意次数,但尽可能少,确保后面是1z;
literally字面上地(.{3})
Capture any character exactly 3 times into capture group 1 (.{3})
任意字符准确捕获 3 次进入捕获组 1If the options vary in length, you'll have to capture in different groups as seen here :如果选项的长度不同,则必须按不同的组进行捕获,如下所示:
(?m)^(?:.*?(abc)|.*?(a.c)|.*?(def)|.*?(%#@)|.*?(1z;))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.