2013-02-26 666 views
1

我想从文本中区分大小写匹配。在下面的例子中,我尝试使用re.search匹配“Ca.iNy”,我想匹配其中“C”应该是大写的,并且所有字符都可以在任何情况下保留。如果它匹配我想要为变量设置一个值的情况。使用python re.search的区分大小写匹配

我已经采取了SO的帮助形式,并通过检查第一个字母是否是大写字母来实现,并且它对于单个检查工作正常。

s = "The details belong to (Ca.iNy.) in this case" 
reg = re.compile("Ca.iny.", re.I) 
reg.search(s).group().startswith("C"). 

但是,我不能在“if else循环”中使用它。我尝试了下面的代码,但搜索似乎不区分大小写。任何人都可以请让我?

import re 

st = "The details belong to (Ca.iNy.) in this case" 
Mval = '' 

if re.search(r"C(?i)a.iny", st): 
    Mval = "AAAAA" 
elif re.search(r"(?i)Ky.", st): 
    Mval = "BBBBB" 
elif re.search(r"(?i)M.sa.", st): 
    Mval = "CCCCC" 
else: 
    Mval = "DDDDD" 

print Mval 
+0

可能重复的[Python的RE(在字,检查第一个字母是大小写敏感的,其余全部不区分大小写)] (http://stackoverflow.com/questions/15071416/python-re-in-a-word-to-check-first-letter-is-case-sensitive-and-rest -all-case) – geoffspear 2013-02-26 16:39:50

+0

@Wooble。那里的答案只涉及一个案例。但这里的问题是在执行if else循环期间。因为如果需要匹配50个左右的情况下使用,否则。如果我最初编译所有模式,将不会好。 – 2013-02-26 16:42:41

+0

@Wooble你可以取消重复的标记吗?这样你们中的任何一个人都可以帮助我实现这一目标? – 2013-02-26 16:48:04

回答

0
import re 

st = "The details belong to (Ca.iNy.) in this case" 
Mval = '' 

if re.search(r"C""(?i)a.iny", st): 
    Mval = "AAAAA" 
elif re.search(r"(?i)Ky.", st): 
    Mval = "BBBBB" 
elif re.search(r"(?i)M.sa.", st): 
    Mval = "CCCCC" 
else: 
    Mval = "DDDDD" 

print Mval 
+1

如果您包含一些文本,这个答案会更有用。试着解释你做了什么来解决问题,以及为什么。 – 2013-02-26 17:03:57

+0

@jeff我没有得到你。你能否让我知道我需要在代码中更改哪些内容? – 2013-02-26 18:19:50

+0

@BryanOakley你能帮我实施吗? – 2013-02-26 18:30:48

0
import re 

st = "The details belong to (Ca.iNy.) in this case" 
Mval = '' 

if re.search(r"C[a-z].[a-z][a-z]", st): # Only change 
    Mval = "AAAAA" 
elif re.search(r"(?i)Ky.", st): 
    Mval = "BBBBB" 
elif re.search(r"(?i)M.sa.", st): 
    Mval = "CCCCC" 
else: 
    Mval = "DDDDD" 

print Mval 
+0

如果您包含一些文本,这个答案会更有用。试着解释你做了什么来解决问题,以及为什么。 – 2013-02-26 17:04:50

+0

对不起,没有评论的是一个意外的帖子。 – user2112190 2013-02-26 17:05:32

+0

为了便于阅读,此代码仅包含[az]。[az] [az]而不是r“C”“(?i)a.iny”,st),并将以下字母设置为小写 – user2112190 2013-02-26 17:06:18

0
import re 
All = {"CA.ing": 3, "cA.aec": 10} 
st = "The details belong to (Ca.inY.) in this case" 
Mval = '' 
class1 = r"[a-z][a-z].[a-z][a-z][a-z]" 
class2 = r"[a-z][a-z][a-z][a-z][a-z][a-z]" # For strings like alaska 

if re.search(class1, st, flags=re.IGNORECASE): 
    found_value = re.search(class1, flags=re.IGNORECASE).group() 
    if found_value in All.keys(): 
     Mval = All[found_value] 
elif re.search(class2, st): 
    found_value = re.search(class2, st).group() 
    if found_value in All.keys(): 
     Mval = All[found_value] 

#This will return a KeyError if a string is present not in your dictionary 
#Note : You can take care of the different case sensitive cases in the dictionary, by 
# only including the proper cases 
+0

是否有可能将预期的组分成分类? – user2112190 2013-02-26 18:50:21

+0

喜欢,有些是6字长,有的有“。”分隔符“?这将帮助你不需要一个疯狂的长长的if/else语句列表 – user2112190 2013-02-26 18:50:52

+0

首先感谢你的答案和帮助,我希望它很难有一个预期的组分类 – 2013-02-26 19:04:31

0
[mport re 

reg = re.compile('([a-z]{2})\.[a-z]{3}',re.I) 

def code(X,r): 
    for ma in r.finditer(X): 
     if ma.group(1)[0].upper()==ma.group(1)[0]: 
      GV = 'OK' 
     else: 
      GV = '- bad match -' 
     yield ' {!s:10} {!s:^13}'.format(ma.group(), GV) 

data = ('Ca.imo Ca.IMo gggg Ca.iMo CL.icv cl.icv cL.icv' 
     'hhhh ca.ghc Rl.axp bbb Rl.AXp nm.fgt') 

print '\n'.join(res for res in code(data,reg)) 

结果

Ca.imo   OK  
    Ca.IMo   OK  
    Ca.iMo   OK  
    CL.icv   OK  
    cl.icv  - bad match - 
    cL.icv  - bad match - 
    ca.ghc  - bad match - 
    Rl.axp   OK  
    Rl.AXp   OK  
    nm.fgt  - bad match -