是否有可能在Python實現的東西像這樣簡單的一個:如何在Python中爲正則表達式的一部分設置ignorecase標誌?
在字符串中間令牌#!/usr/bin/perl
my $a = 'Use HELLO1 code';
if($a =~ /(?i:use)\s+([A-Z0-9]+)\s+(?i:code)/){
print "$1\n";
}
字母總是大寫。其他字母的字母可以有任何情況(USE,使用,使用,代碼,代碼,代碼等)
是否有可能在Python實現的東西像這樣簡單的一個:如何在Python中爲正則表達式的一部分設置ignorecase標誌?
在字符串中間令牌#!/usr/bin/perl
my $a = 'Use HELLO1 code';
if($a =~ /(?i:use)\s+([A-Z0-9]+)\s+(?i:code)/){
print "$1\n";
}
字母總是大寫。其他字母的字母可以有任何情況(USE,使用,使用,代碼,代碼,代碼等)
至於我能找到,蟒蛇正則表達式引擎不支持部分ignore-案件。這是一個使用不區分大小寫的正則表達式的解決方案,然後測試後面的標記是否爲大寫。
#! /usr/bin/env python
import re
token_re = re.compile(r'use\s+([a-z0-9]+)\s+code', re.IGNORECASE)
def find_token(s):
m = token_re.search(s)
if m is not None:
token = m.group(1)
if token.isupper():
return token
if __name__ == '__main__':
for s in ['Use HELLO1 code',
'USE hello1 CODE',
'this does not match',
]:
print s, '->',
print find_token(s)
這裏是程序的輸出:
Use HELLO1 code -> HELLO1
USE hello1 CODE -> None
this does not match -> None
According to the docs,這是不可能的。 (?x)
語法只允許您修改整個表達式的標誌。因此,你必須拆分此爲三個正則表達式,並應用它們一前一或後做了人工「忽略大小寫」:/[uU][sS][eE]...
我看到,thx。希望死亡最後:) – 2009-09-21 15:48:26
因爲Python 3.6,你可以使用標誌的內部組:
(?imsx-imsx:...)
(來自'i','m','s','x'的零個或多個字母,可選地後跟' - ',後跟一個或多個來自同一組的字母。)這些字母設置或移除相應的f ags:re.I(忽略大小寫),re.M(多行),re.S(點全部匹配)和re.X(冗長),表示部分。
因此(?i:use)
現在是一個正確的語法。從python3.6終端:
>>> import re
>>> regex = re.compile('(?i:use)\s+([A-Z0-9]+)\s+(?i:code)')
>>> regex.match('Use HELLO1 code')
<_sre.SRE_Match object; span=(0, 15), match='Use HELLO1 code'>
>>> regex.match('use HELLO1 Code')
<_sre.SRE_Match object; span=(0, 15), match='use HELLO1 Code'>
'match'顯然不是正確的方法 - 如果你切換到'search',因爲你不循環尋找「下一個可能的候選人」,你的'find_token'會給出錯誤的否定結果如果「除案例之外的好」的例子在「好包括案例」之一之前)。 – 2009-09-21 16:07:42
@Alex Martelli:謝謝。搜索更好,你是對的。固定。 – 2009-09-21 16:25:12