如何在Python中将字符串与占位符进行比较

时间:2015-10-06 17:56:52

标签: python python-2.7 python-3.x

假设你有一个这样的字符串:

"ERROR: Error Number %d, Error Location 0x%x, found exception"

假设您编写了一个程序来搜索文本文件以查找这个确切的字符串,但您不关心占位符%d%x,但您要确保正确匹配字符串。你会怎么做呢?

有人可能会想到为什么不只是比较子字符串:"ERROR: Error Number" ...,那么假设文本文件中有其他字符串具有相同的起始子字符串"ERROR: Error Number",但是你没有想抓住那些。

2 个答案:

答案 0 :(得分:1)

使用正则表达式。 在您的情况下,您可能希望使用以下内容:

http://pythex.org/?regex=ERROR%3A%5CsError%5CsNumber%5Cs(%5Cd%2B)%2C%5CsError%5CsLocation%5Cs0x(%5Cd%2B)%2C%5Csfound%5Csexception&test_string=ERROR%3A%20Error%20Number%2032%2C%20Error%20Location%200x420%2C%20found%20exception&ignorecase=0&multiline=0&dotall=0&verbose=0

示例:

ggplot(data, aes(x=type, y=point, fill="type")) +
geom_bar(stat="identity") + 
geom_text(data=data, aes(label=paste(type,"-",point))) + 
scale_y_log10(labels =  format_format(scientific = FALSE)) 

答案 1 :(得分:1)

再次使用RegEx

您必须将格式转换为RegEx:

  • 首先,转义非字母数字字符;
  • 用相应的RegEx替换每种格式(例如:%d)(例如:\d+);
  • 添加^$运算符以确保完全匹配;
  • 编译RegEx以进行优化;
  • 使用matchfindall

以下是一个例子:

import re

my_format = "ERROR: Error Number %d, Error Location 0x%x, found exception"

# Escape all non-alphanumeric characters in pattern
my_regex = re.escape(my_format)

# Mapping: pattern => regex
mapping = [(r"\%d", r"\d+"),
           (r"\%x", r"[0-9a-f]+")]

# Substitute each pattern by regex
for pattern, regex in mapping:
    my_regex = my_regex.replace(pattern, regex)

# Add begin/end operator for exact match
my_regex = "^" + my_regex + "$"
print(my_regex)

# Compile the RegEx, extract the 'match' function
match_my_regex = re.compile(my_regex, re.DOTALL).match

samples = ["789",
           "ERROR: Error Number 123, Error Location 0xaf, found exception",
           "ERROR: Error Number 456, Error Location 0xa0, found exception",
           "Got ERROR: Error Number 123, Error Location 0xaf, found exception"]

for sample in samples:
    print("{0}: match => {1}".format(sample, match_my_regex(sample) is not None))

您将获得:

789: match => False
ERROR: Error Number 123, Error Location 0xaf, found exception: match => True
ERROR: Error Number 456, Error Location 0xa0, found exception: match => True
Got ERROR: Error Number 123, Error Location 0xaf, found exception: match => False