【问题标题】:Case sensitive match using python re.search使用 python re.search 区分大小写匹配
【发布时间】:2013-02-26 16:35:07
【问题描述】:

我想对文本进行区分大小写的匹配。在以下情况下,我尝试使用 re.search 匹配“Ca.iNy”,我想匹配“C”应该大写的位置,其余所有字符都可能在任何情况下。如果它匹配我想为变量设置一个值的情况。

我采用了 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

【问题讨论】:

标签: python regex


【解决方案1】:
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

【讨论】:

  • 如果您包含一些文字,此答案会更有用。尝试解释您为解决问题所做的工作以及原因。
  • @jeff 我没听懂你。你能告诉我我需要在哪里更改代码吗?
  • @BryanOakley 你能帮我实现这个吗?
【解决方案2】:
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

【讨论】:

  • 如果您包含一些文字,此答案会更有用。尝试解释您为解决问题所做的工作以及原因。
  • 很抱歉,没有评论的是意外发帖。
  • 这个,只有 [a-z].[a-z][a-z] 放置而不是 r"C""(?i)a.iny", st) ,以提高可读性,并使以下字母小写
  • 它是原始的,但搜索在最后移动到 else。
  • @user2112190 但是考虑一个匹配“Ca.iRo”的情况。我需要将 Mval 设置为 "DDDDD" 并且如果匹配 "Ca.IPl Vn.Lp" 。我需要将 Mval 设置为“FFFFF”,我需要将这些条件保持在 50 左右。
【解决方案3】:
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

【讨论】:

  • 是否可以将预期的组划分为分类?
  • 就像,有些是 6 个字符长,有些有一个“。”分隔符”?这将帮助您不再需要一长串的 if/else 语句
  • 首先感谢您的回答和帮助。我希望它很难有一个预期的组分类。
  • 好吧,假设你能做到,我会再上传一个可能有帮助的答案,附近的 cmets 应该会有所帮助,并且作为说明,哪些案例适用于案例非常重要敏感度,哪些不是
  • 我猜这应该可行。如果我需要进一步的帮助,我会告诉你的。再次感谢您的回答。
【解决方案4】:
[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 -

【讨论】:

    猜你喜欢
    • 2022-06-13
    • 2017-11-14
    • 2012-03-15
    • 2012-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-30
    • 2013-10-19
    相关资源
    最近更新 更多