Bez rozróżniania wielkości liter Wyrażenie regularne bez re./ align = "left" /
W Pythonie mogę skompilować Wyrażenie regularne bez rozróżniania wielkości liter używając 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>
Czy istnieje sposób, aby zrobić to samo, ale bez użycia re.compile
. Nie mogę znaleźć niczego podobnego do przyrostka i
Perla (np. m/test/i
) w dokumentacji.
9 answers
Pass re.IGNORECASE
to flags
param of search
, match
, lub sub
:
re.search('test', 'TeSt', re.IGNORECASE)
re.match('test', 'TeSt', re.IGNORECASE)
re.sub('test', 'xxxx', 'Testing', flags=re.IGNORECASE)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-06-26 11:26:40
Możesz również wyszukiwać bez rozróżniania wielkości liter używając opcji search / match bez flagi IGNORECASE (testowanej w Pythonie 2.7.3):
re.search(r'(?i)test', 'TeSt').group() ## returns 'TeSt'
re.match(r'(?i)test', 'TeSt').group() ## returns 'TeSt'
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-05-04 07:05:38
Znacznik niewrażliwy na wielkość liter, (?i)
może być wbudowany bezpośrednio do wzoru regex:
>>> import re
>>> s = 'This is one Test, another TEST, and another test.'
>>> re.findall('(?i)test', s)
['Test', 'TEST', 'test']
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-02-23 03:43:41
Można również zdefiniować wielkość liter podczas kompilacji wzorca:
pattern = re.compile('FIle:/+(.*)', re.IGNORECASE)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-06-26 11:32:34
W imporcie
import re
W czasie wykonywania przetwarzania:
RE_TEST = r'test'
if re.match(RE_TEST, 'TeSt', re.IGNORECASE):
Należy wspomnieć, że nie używanie re.compile
jest marnotrawstwem. Za każdym razem, gdy zostanie wywołana powyższa metoda match, zostanie skompilowane Wyrażenie regularne. Jest to również błędna praktyka w innych językach programowania. Poniżej jest lepsza praktyka.
In App initialization:
self.RE_TEST = re.compile('test', re.IGNORECASE)
W czasie wykonywania przetwarzania:
if self.RE_TEST.match('TeSt'):
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-02-07 15:49:07
Aby wykonać operacje niewrażliwe na wielkość liter, podaj re.IGNORECASE
>>> import re
>>> test = 'UPPER TEXT, lower text, Mixed Text'
>>> re.findall('text', test, flags=re.IGNORECASE)
['TEXT', 'text', 'Text']
I jeśli chcemy zastąpić tekst pasujący do sprawy...
>>> 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'
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-06-21 04:15:43
#'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'>
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-06-29 21:27:36
Jeśli chcesz zastąpić, ale nadal zachowując styl poprzedniego str. To możliwe.
Na przykład: wyróżnij łańcuch "test asdasd TEST ASD tEst asdasd".
sentence = "test asdasd TEST asd tEst asdasd"
result = re.sub(
'(test)',
r'<b>\1</b>', # \1 here indicates first matching group.
sentence,
flags=re.IGNORECASE)
Test asdasd TEST asd tEst asdasd
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-11-04 05:19:44
Dla wyrażenia regularnego niewrażliwego na wielkość liter (Regex): Są dwa sposoby dodawania w kodzie:
-
flags=re.IGNORECASE
Regx3GList = re.search("(WCDMA:)((\d*)(,?))*", txt, **re.IGNORECASE**)
-
Znacznik niewrażliwy na wielkość liter
(?i)
Regx3GList = re.search("**(?i)**(WCDMA:)((\d*)(,?))*", txt)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-05-04 20:56:13