python - 正则表达式匹配直到给定文本?

标签 python regex

我正在编写一个匹配 fooSVM??! 的正则表达式。我想在这个字符串中匹配 SVM,这意味着我需要从 foo 开始匹配,直到遇到 ?!。请注意,SVM 可以是其他 Unicode 字符。

我尝试了 foo(.*)[?!]* 但似乎没有用。

最佳答案

假设不能有换行符,这会做到:

foo([^?!]+)[?!]

并检索第一组。

foo        # Find "f", then "o", then "o",
(          # begin capture
    [^?!]  # followed by any character which is not "!" or "?",
    +      # once or more
)          # end capture
[?!]       # followed by either a "!" or a "?"

关于python - 正则表达式匹配直到给定文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14283729/

相关文章:

c# - 如果一组符号仅重复,如何使正则表达式匹配?

python - 使用正则表达式从网页中提取表格

python - 如何在 keras 模型上添加一些属性?

python - Factory Boy 随机选择字段选项 "choices"

python - 从对象扩展类

python - 我在我的代码中使用了 Jinja2 "recursive"标签,但是我怎样才能得到当前循环的深度呢?

regex - 正则表达式对面

mysql - 将正则表达式与 in、MySQL 结合

php - 使用正则表达式删除 HTML 标签

python - 用Python编写正则表达式