python重新编译匹配百分比符号%
我想在Python中为%符号创建一个简单的字符串匹配。python重新编译匹配百分比符号%,python,regex,Python,Regex,我想在Python中为%符号创建一个简单的字符串匹配。 这是我的密码 import re a = "5%" p = re.compile(r'%') p.match(a) p、 match(a)返回None re.match方法仅匹配位于字符串开头的模式。你的不是。尝试re.search,它会在字符串中的任何位置找到模式。match如果出现在搜索字符串的开头,则匹配regexp。您需要p.search(a)以下是另一种样式: for cell in row: cell_
这是我的密码
import re
a = "5%"
p = re.compile(r'%')
p.match(a)
p、 match(a)返回None re.match方法仅匹配位于字符串开头的模式。你的不是。尝试
re.search
,它会在字符串中的任何位置找到模式。match
如果出现在搜索字符串的开头,则匹配regexp。您需要p.search(a)
以下是另一种样式:
for cell in row:
cell_val = cell.value
if bool(re.search(r'% grade', cell_val)):
print("Yup, found it")