假设你有一个这样的字符串:
"ERROR: Error Number %d, Error Location 0x%x, found exception"
假设您编写了一个程序来搜索文本文件以查找这个确切的字符串,但您不关心占位符%d
,%x
,但您要确保正确匹配字符串。你会怎么做呢?
有人可能会想到为什么不只是比较子字符串:"ERROR: Error Number"
...,那么假设文本文件中有其他字符串具有相同的起始子字符串"ERROR: Error Number"
,但是你没有想抓住那些。
答案 0 :(得分:1)
使用正则表达式。 在您的情况下,您可能希望使用以下内容:
示例:
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:
%d
)(例如:\d+
); ^
和$
运算符以确保完全匹配; match
,findall
等以下是一个例子:
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