Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.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 在查询中编写等价于like语句的正则表达式_Python_Regex - Fatal编程技术网

Python 在查询中编写等价于like语句的正则表达式

Python 在查询中编写等价于like语句的正则表达式,python,regex,Python,Regex,我有一个示例查询 select c.name , c.id from company c where c.name like 'Hoder%' and c.name like 'Stock%' amd c.name like 'Colour%' and c.name like '%Delete' and c.name like '%Regular' and c.name like '%Filler' and c.name like '%wipe%' and c.name like '%p

我有一个示例查询

 select c.name , c.id from company c where
 c.name like 'Hoder%' and c.name like 'Stock%' amd c.name like 'Colour%'
 and c.name like '%Delete' and c.name like '%Regular' and c.name like '%Filler'
 and c.name like '%wipe%' and c.name like '%palce%' and c.name like '%double%',
 and c.name in ('IT', 'Wall', SpyCorp', 'Signal')
从上面的查询中我知道了单词列表,我正在用正则表达式实现查询 上面的表格我写了一个正则表达式

#for exact matching words 
pnexact = ['IT', 'Wall', SpyCorp', 'Signal']
regexact = [re.compile(r"\b%s\b" % word.lower() ) for word in pnexact]#list of regex object
#for words which are contain in the string eg: '%wipe%' ...
pncontains = ['wipe','palce','double',]
pnregexobj_contains = re.compile(r"%s" % '|'.join(pncontains)) #single regex object
上述正则表达式工作正常,但我无法编写正则表达式,它将处理单词
'Hoder%'、'Stock%'、'Color%'
案例
%Delete'、'%Regular'、'%Filler'
请向我推荐处理上述两种情况的regex


谢谢

%
替换为
*
。例如,要匹配
%Delete
等词,请使用:

>>> import re
>>> re.match(r'\b.*Delete\b', 'NoDelete')
<_sre.SRE_Match object at 0x100456100>
>>> _.group()
'NoDelete'
>>> re.match(r'\b.*Delete\b', 'Mismatch')
>>重新导入
>>>重新匹配(r'\b.*删除\b','NoDelete')
>>>(组)
“诺德莱特”
>>>重新匹配(r'\b.*删除\b',“不匹配”)
要确定
name
是否为单词之一:

pnexact = ['IT', 'Wall', 'SpyCorp', 'Signal'] # use `set()` if the list is long
name in pnexact # name in ('IT', 'Wall', 'SpyCorp', 'Signal')
或者如果您想使用正则表达式:

m(r"^(?:%s)$" % "|".join(map(re.escape, pnexact)))

这部分:
其中c.name如“Hoder%”和c.name如“Stock%”

相当于:
其中False

列(如
c.name
)不能同时从
Hoder
Stock
开始


您可能想使用
而不是

我为“%delete”编写了这个regex reg=re.compile(r'(\.*)delete$)字符串以单词delete结尾。可以吗?我想您不想对
进行反斜杠转义,因为这会将其转换为文本
字符,但是,与诸如“%Delete”
之类的
等价的正确正则表达式应该是
*Delete$
,或者仅仅是
Delete$
m(r"^(?:%s)$" % "|".join(map(re.escape, pnexact)))