我的正则表达式无法正常工作 [英] My regex is not working properly

查看:38
本文介绍了我的正则表达式无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的正则表达式工作不正常.我正在向您展示 before regex textafter regex text.我正在使用这个正则表达式 re.search(r'(?ms).*?{{(Infobox film.*?)}}', text).你会看到我的正则表达式在 | 之后没有显示结果country = 阿萨姆邦,{{IND .我的正则表达式卡在这一点上.请你帮帮我好吗?谢谢

在正则表达式之前:

<前>{{信息框影片|名称 = 帕波里|发布 = 1986|运行时间 = 144 分钟|国家 = 阿萨姆邦,{{IND}}|语言 = [[阿萨姆语|阿萨姆语]]|预算 =|后跟 = 免费}}

在正则表达式之后:

<前>{信息框影片|名称 = 帕波里|发布 = 1986|运行时间 = 144 分钟|国家 = 阿萨姆邦,{{IND

为什么正则表达式卡在这一点上?country = 阿萨姆邦,{{IND

期待结果

<前>信息箱贴膜|名称 = 帕波里|发布 = 1986|运行时间 = 144 分钟|国家 = 阿萨姆邦,{{IND}}|语言 = [[阿萨姆语|阿萨姆语]]|预算 =|后跟 = 免费

解决方案

您的正则表达式正在捕获第一个 {{ 和第一个 }} 之间的所有内容,它位于信息框的国家"条目.如果您想要第一个 {{last }} 之间的所有内容,那么您想要制作 .*> 通过删除 ? 在大括号内贪婪:

re.search(r'(?ms).*?{{(Infobox film.*)}}', text)

请注意,这将在输入中找到 last }}(例如,如果在信息框的末尾下方还有另一个模板,它将找到那),所以这可能不是你想要的.当您有这样的嵌套时,正则表达式并不总是最好的搜索方式.

My regex is not working properly. I'm showing you before regex text and after regex text. I'm using this regex re.search(r'(?ms).*?{{(Infobox film.*?)}}', text). You will see my regex not displaying the result after | country = Assam, {{IND . My regex stuck at this point. Will you please help me ? thanks

Before regex:

    
{{Infobox film
| name           = Papori
| released       = 1986
| runtime        = 144 minutes
| country        = Assam, {{IND}}
| language       = [[Assamese language|Assamese]]
| budget         = 
| followed by    = free
}}

After regex:

    
{Infobox film
| name           = Papori
| released       = 1986
| runtime        = 144 minutes
| country        = Assam, {{IND

Why regex stuck at this point? country = Assam, {{IND

Edit : Expecting Result

Infobox film
    | name           = Papori
    | released       = 1986
    | runtime        = 144 minutes
    | country        = Assam, {{IND}}
    | language       = [[Assamese language|Assamese]]
    | budget         = 
    | followed by    = free

解决方案

Your regex is catching everything between the first {{ and the first }}, which is in the "country" entry of the infobox. If you want everything between the first {{ and the last }}, then you want to make the .* inside the braces greedy by removing the ?:

re.search(r'(?ms).*?{{(Infobox film.*)}}', text)

Note that this will find the last }} in the input (eg. if there's another template far below the end of the infobox, it will find the end of that), so this may not be what you want. When you have nesting things like this, regex is not always the best way to search.

这篇关于我的正则表达式无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆