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

时间:2013-01-10 17:17:19

标签: 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

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

/(?=.*[3])(foo)|(bar)/
  • 如果字符串以3结尾
  • 然后返回foo
  • 其他返回栏

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

1 个答案:

答案 0 :(得分:8)

使用轮换时你走在正确的轨道上,但你不需要前瞻。

/.*(foo).*3$|.*(bar).*2$|.*(foobar).*/

你必须回来:

$1$2$3

我拥有所有.*因为我假设您使用foobar作为占位符。

相关问题