如何在python中解析带有'+'的标签 [英] How to parse for tags with '+' in python

查看:36
本文介绍了如何在python中解析带有'+'的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试编译这个时,我收到了一个没有重复"的错误:

I'm getting a "nothing to repeat" error when I try to compile this:

search = re.compile(r'([^a-zA-Z0-9])(%s)([^a-zA-Z0-9])' % '+test', re.I)

问题是+"号.我该如何处理?

The problem is the '+' sign. How should I handle that?

推荐答案

re.compile(r'([^a-zA-Z0-9])(%s)([^a-zA-Z0-9])' % '\+test', re.I)

+"是正则表达式中的至少重复一次"量词.它必须遵循可重复的内容,或者如果您想匹配文字+",则必须对其进行转义.

The "+" is the "repeat at least once" quantifier in regular expressions. It must follow something that is repeatable, or it must be escaped if you want to match a literal "+".

如果您想动态构建正则表达式,这样更好.

Better is this, if you want to build your regex dynamically.

re.compile(r'([^a-zA-Z0-9])(%s)([^a-zA-Z0-9])' % re.escape('+test'), re.I)

这篇关于如何在python中解析带有'+'的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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