Mat*_*Mat 289 python regex case-sensitive case-insensitive
在Python中,我可以使用以下命令编译正则表达式以区分大小写re.compile:
>>> s = 'TeSt'
>>> casesensitive = re.compile('test')
>>> ignorecase = re.compile('test', re.IGNORECASE)
>>>
>>> print casesensitive.match(s)
None
>>> print ignorecase.match(s)
<_sre.SRE_Match object at 0x02F0B608>
Run Code Online (Sandbox Code Playgroud)
有没有办法做同样的事情,但没有使用re.compile.我在文档中找不到类似Perl的i后缀(例如m/test/i).
Mic*_*ren 498
传递re.IGNORECASE到flags的PARAM search,match或sub:
re.search('test', 'TeSt', re.IGNORECASE)
re.match('test', 'TeSt', re.IGNORECASE)
re.sub('test', 'xxxx', 'Testing', flags=re.IGNORECASE)
Run Code Online (Sandbox Code Playgroud)
aem*_*999 92
您还可以使用搜索/匹配执行不区分大小写的搜索,而不使用IGNORECASE标志(在Python 2.7.3中测试):
re.search(r'(?i)test', 'TeSt').group() ## returns 'TeSt'
re.match(r'(?i)test', 'TeSt').group() ## returns 'TeSt'
Run Code Online (Sandbox Code Playgroud)
Ray*_*ger 46
不区分大小写的标记(?i)可以直接合并到正则表达式模式中:
>>> import re
>>> s = 'This is one Test, another TEST, and another test.'
>>> re.findall('(?i)test', s)
['Test', 'TEST', 'test']
Run Code Online (Sandbox Code Playgroud)
小智 12
对于不区分大小写的正则表达式(Regex):有两种方法可以在代码中添加:
flags=re.IGNORECASE
Regx3GList = re.search("(WCDMA:)((\d*)(,?))*", txt, re.IGNORECASE)
Run Code Online (Sandbox Code Playgroud)
不区分大小写的标记(?i)
Regx3GList = re.search("**(?i)**(WCDMA:)((\d*)(,?))*", txt)
Run Code Online (Sandbox Code Playgroud)
pan*_*ish 10
您还可以在模式编译期间定义不区分大小写:
pattern = re.compile('FIle:/+(.*)', re.IGNORECASE)
Run Code Online (Sandbox Code Playgroud)
在进口
import re
Run Code Online (Sandbox Code Playgroud)
在运行时处理:
RE_TEST = r'test'
if re.match(RE_TEST, 'TeSt', re.IGNORECASE):
Run Code Online (Sandbox Code Playgroud)
需要说明的是,不使用re.compile是浪费。每次调用上述匹配方法时,都会编译正则表达式。这在其他编程语言中也是错误的做法。下面是更好的做法。
在应用程序初始化中:
self.RE_TEST = re.compile('test', re.IGNORECASE)
Run Code Online (Sandbox Code Playgroud)
在运行时处理:
if self.RE_TEST.match('TeSt'):
Run Code Online (Sandbox Code Playgroud)
要执行不区分大小写的操作,请提供 re.IGNORECASE
>>> import re
>>> test = 'UPPER TEXT, lower text, Mixed Text'
>>> re.findall('text', test, flags=re.IGNORECASE)
['TEXT', 'text', 'Text']
Run Code Online (Sandbox Code Playgroud)
如果我们想替换匹配大小写的文本......
>>> def matchcase(word):
def replace(m):
text = m.group()
if text.isupper():
return word.upper()
elif text.islower():
return word.lower()
elif text[0].isupper():
return word.capitalize()
else:
return word
return replace
>>> re.sub('text', matchcase('word'), test, flags=re.IGNORECASE)
'UPPER WORD, lower word, Mixed Word'
Run Code Online (Sandbox Code Playgroud)
#'re.IGNORECASE' for case insensitive results short form re.I
#'re.match' returns the first match located from the start of the string.
#'re.search' returns location of the where the match is found
#'re.compile' creates a regex object that can be used for multiple matches
>>> s = r'TeSt'
>>> print (re.match(s, r'test123', re.I))
<_sre.SRE_Match object; span=(0, 4), match='test'>
# OR
>>> pattern = re.compile(s, re.I)
>>> print(pattern.match(r'test123'))
<_sre.SRE_Match object; span=(0, 4), match='test'>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
265415 次 |
| 最近记录: |