regex - 在 if-elsif-else-end 中使用正则表达式

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

我找到了 (?(?=condition)(then1|then2|then3)|(else1|else2|else3))来自 http://www.regular-expressions.info/conditional.html但不知道如何让它在这种情况下工作。
这是我的正则表达式 if-then-else-then-end :
/(?=.*[3])(foo)|(bar)/
  • 如果字符串以 3 结尾
  • 然后返回 foo
  • 否则返回栏

  • 我认为可以在父阶段的 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/

    相关文章:

    regex - R 正则表达式 : Parenthesis Not Acting as Metacharacter

    R tidyr 正则表达式 : extract ordered numbers from character column

    java - java.util.regex 引擎中奇怪的正向先行行为

    Notepad++ 背后的正则表达式正面

    php - preg_match : nothing to repeat/no match

    c# - 如何使用 RazorEngine 将 System.Text.RegularExpressions 添加到模板?

    regex - R - 从文件中读取行的部分匹配

    ios - 用于将 URL 与视频 ID 匹配的正则表达式

    javascript - 正则表达式:捕获组不是从一开始就进行否定前瞻

    php - 将字符串分解为字段