Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python重新编译匹配百分比符号%_Python_Regex - Fatal编程技术网

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_

我想在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_val = cell.value
    if bool(re.search(r'% grade', cell_val)):
        print("Yup, found it")