我正在尝试使用 REGEX 来做一个 if-then-elsif-then-else-then-end
例子:
s = "foobar123"
if end of s is 3
then return "foo"
elsif end of s is 2
then return "bar"
else
then return "foobar"
end
我找到了
(?(?=condition)(then1|then2|then3)|(else1|else2|else3))
来自 http://www.regular-expressions.info/conditional.html但不知道如何让它在这种情况下工作。这是我的正则表达式
if-then-else-then-end
:/(?=.*[3])(foo)|(bar)/
我认为可以在父阶段的 else 中编写另一个 if-else 但不能:(
最佳答案
你在使用交替的正确轨道上,但你不需要前瞻。
/.*(foo).*3$|.*(bar).*2$|.*(foobar).*/
你必须返回:
$1$2$3
我有所有
.*
s 因为我假设您正在使用 foobar
作为占位符。
关于regex - 在 if-elsif-else-end 中使用正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14263515/