在if-elsif-else-end中使用REGEX

yeu*_*nam 4 regex regex-lookarounds

我正在尝试使用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
Run Code Online (Sandbox Code Playgroud)

(?(?=condition)(then1|then2|then3)|(else1|else2|else3))http://www.regular-expressions.info/conditional.html找到了,但在这种情况下不知道如何使它工作.这是我的正则表达式if-then-else-then-end:

/(?=.*[3])(foo)|(bar)/
Run Code Online (Sandbox Code Playgroud)
  • 如果字符串以3结尾
  • 然后返回foo
  • 否则返回吧

我认为可以写另一个if-else在其他父级,但不能:(

And*_*ong 8

您使用交替进入正确的轨道,但您不需要前瞻.

/.*(foo).*3$|.*(bar).*2$|.*(foobar).*/
Run Code Online (Sandbox Code Playgroud)

你必须返回:

$1$2$3
Run Code Online (Sandbox Code Playgroud)

我有所有的.*s因为我假设你foobar用作占位符.