2013-02-26 44 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 - 
相關問題