代码之家  ›  专栏  ›  技术社区  ›  Joe Ijam

Regex获取句子中的文本

  •  -5
  • Joe Ijam  · 技术社区  · 7 年前

    下面是我在语言配置中的一条消息。我想检测可能被随机选择以显示给用户的单词。

    什么是正则表达式,以检测我加粗时随机单词的可能性

    [好极了|恭喜|干得好| Hoorey | Yeehaa |哇] ! 现在,您将成为赢家。继续前进!

    Php开发

    2 回复  |  直到 7 年前
        1
  •  1
  •   ctwheels    7 年前

    密码

    See regex in use here

    ^[^!]+(?=!)
    

    后果

    输入

    Bravo! You are now to top to be a winner. Keep going!
    Great! You are now to top to be a winner. Keep going!
    Congratulations! You are now to top to be a winner. Keep going!
    Well done! You are now to top to be a winner. Keep going!
    Hooray! You are now to top to be a winner. Keep going!
    Yeehaa! You are now to top to be a winner. Keep going!
    Wow! You are now to top to be a winner. Keep going!
    

    输出

    Bravo
    Great
    Congratulations
    Well done
    Hoorey
    Yeehaa
    Wow
    

    解释

    • ^ 在行首断言位置
    • [^!]+ 匹配除 ! 一次或多次
    • (?=!) 积极前瞻确保匹配后的内容 ! 字面上
        2
  •  0
  •   Philip    7 年前

    选择要显示的单词 ,我不确定您是否需要正则表达式。我会这样做:

    function getMessage () {
        $words = ["Bravo","Great","Congratulations","Well done","Hoorey","Yeehaa","Wow"];
        return $words [rand (0, count ($words) - 1] . "! You are now to top to be a winner. Keep going!";
    }