python re用于精确匹配数字和字符串

python re用于精确匹配数字和字符串,python,python-2.7,Python,Python 2.7,我试图匹配一个字符串,如果它完全匹配,而忽略大小写。下面是我的字符串值不同但仍然匹配的代码 import re k = "999" v = "99" if (re.search(v, k , re.IGNORECASE)): print "xyz" k = "AAA" v = "aa" if (re.search(v, k , re.IGNORECASE)): print "xyz" 在上述代码中,k=999,v=99,但匹配且k=AAA,v=aa匹配。我真正需要的是,如果k=

我试图匹配一个字符串,如果它完全匹配,而忽略大小写。下面是我的字符串值不同但仍然匹配的代码

import re
k = "999"
v = "99"
if (re.search(v, k , re.IGNORECASE)):
   print "xyz" 
k = "AAA"
v = "aa"
if (re.search(v, k , re.IGNORECASE)):
   print "xyz" 

在上述代码中,k=999,v=99,但匹配且
k=AAA,v=aa
匹配。我真正需要的是,如果
k=999
v=999
匹配,那么其他所有情况都不应该匹配。如果
k=AAA
v=aa
不匹配,则wise
k=AAA
v=aa
应该匹配(意思是忽略大小写)。

你的意思是<代码>如果k==v:?

你的意思是
如果k==v:

不确定为什么需要正则表达式,但无论出于什么原因,如果需要,您都可以使用字符串的开头和结尾进行匹配

k = re.compile(r"^99$")

不确定为什么需要正则表达式,但无论出于何种原因,如果需要,您都可以使用字符串的开头和结尾进行匹配

k = re.compile(r"^99$")

进行不区分大小写比较的标准方法是使用
lower()
upper()

代码:

def matches(str1, str2):
    return 'matches' if str1.lower() == str2.lower() else 'does not match'
data = (
    ("999", "99"),
    ("999", "999"),
    ("999X", "999x"),
    ("999Xx", "999x"),
)

def matches(str1, str2):
    return 'matches' if str1.lower() == str2.lower() else 'does not match'

for datum in data:
    print('%s %s %s' % (datum[0], matches(*datum), datum[1]))
999 does not match 99
999 matches 999
999X matches 999x
999Xx does not match 999x
测试代码:

def matches(str1, str2):
    return 'matches' if str1.lower() == str2.lower() else 'does not match'
data = (
    ("999", "99"),
    ("999", "999"),
    ("999X", "999x"),
    ("999Xx", "999x"),
)

def matches(str1, str2):
    return 'matches' if str1.lower() == str2.lower() else 'does not match'

for datum in data:
    print('%s %s %s' % (datum[0], matches(*datum), datum[1]))
999 does not match 99
999 matches 999
999X matches 999x
999Xx does not match 999x
结果:

def matches(str1, str2):
    return 'matches' if str1.lower() == str2.lower() else 'does not match'
data = (
    ("999", "99"),
    ("999", "999"),
    ("999X", "999x"),
    ("999Xx", "999x"),
)

def matches(str1, str2):
    return 'matches' if str1.lower() == str2.lower() else 'does not match'

for datum in data:
    print('%s %s %s' % (datum[0], matches(*datum), datum[1]))
999 does not match 99
999 matches 999
999X matches 999x
999Xx does not match 999x

进行不区分大小写比较的标准方法是使用
lower()
upper()

代码:

def matches(str1, str2):
    return 'matches' if str1.lower() == str2.lower() else 'does not match'
data = (
    ("999", "99"),
    ("999", "999"),
    ("999X", "999x"),
    ("999Xx", "999x"),
)

def matches(str1, str2):
    return 'matches' if str1.lower() == str2.lower() else 'does not match'

for datum in data:
    print('%s %s %s' % (datum[0], matches(*datum), datum[1]))
999 does not match 99
999 matches 999
999X matches 999x
999Xx does not match 999x
测试代码:

def matches(str1, str2):
    return 'matches' if str1.lower() == str2.lower() else 'does not match'
data = (
    ("999", "99"),
    ("999", "999"),
    ("999X", "999x"),
    ("999Xx", "999x"),
)

def matches(str1, str2):
    return 'matches' if str1.lower() == str2.lower() else 'does not match'

for datum in data:
    print('%s %s %s' % (datum[0], matches(*datum), datum[1]))
999 does not match 99
999 matches 999
999X matches 999x
999Xx does not match 999x
结果:

def matches(str1, str2):
    return 'matches' if str1.lower() == str2.lower() else 'does not match'
data = (
    ("999", "99"),
    ("999", "999"),
    ("999X", "999x"),
    ("999Xx", "999x"),
)

def matches(str1, str2):
    return 'matches' if str1.lower() == str2.lower() else 'does not match'

for datum in data:
    print('%s %s %s' % (datum[0], matches(*datum), datum[1]))
999 does not match 99
999 matches 999
999X matches 999x
999Xx does not match 999x

你为什么要用正则表达式呢?一个简单的比较给出了您所需要的:
如果v==k:
。我需要忽略大小写,我还将比较stringsSo您不仅要使用您答案中的数据?只是accpet@Stephen Rauch的答案。您为什么要使用regex?一个简单的比较给出了你所需要的:
如果v==k:
。我需要忽略大小写,我也会比较strings你不仅仅会使用你答案中的数据?只需要accpet@Stephen Rauch的答案。我需要忽略大小写,我也会比较strings我需要忽略大小写,我也会比较strings我需要忽略大小写,我还将比较字符串我需要忽略大小写,我还将比较字符串