Python中的通配符匹配 [英] Wildcard matching in Python
问题描述
我有一个叫做Pattern的类,其中有两个方法equates和setwildcard. Equates返回索引,在该索引中子字符串首先出现在字符串中,而setwildcard在子字符串中设置通配符
I have a class called Pattern, and within it two methods, equates and setwildcard. Equates returns the index in which a substring first appears in a string, and setwildcard sets a wild card character in a substring
所以
p = Pattern('xyz')
t = 'xxxxxyz'
p.equates(t)
返回4
也
p = Pattern('x*z', '*')
t = 'xxxxxgzx'
p.equates(t)
返回4,因为*是通配符,并且可以匹配t内的任何字母,只要x和z匹配即可. 实现此目的的最佳方法是什么?
Returns 4, because * is the wildcard and can match any letter within t, as long as x and z match. What's the best way to implement this?
推荐答案
看起来您实际上是在实现正则表达式的子集.幸运的是,Python有一个内置库!如果您不熟悉正则表达式(或他们的朋友称其为正则表达式)的工作方式,强烈建议您通读
It looks like you're essentially implementing a subset of regular expressions. Luckily, Python has a library for that built-in! If you're not familiar with how regular expressions (or, as their friends call them, regexes) work, I highly recommend you read through the documentation for them.
无论如何,我认为函数re.search
正是您要寻找的.它以匹配的模式作为第一个参数,并以匹配的字符串作为第二个参数.如果模式匹配,则search
返回一个SRE_Match
对象,该对象通常具有#start()
方法,该方法返回匹配开始的索引.
In any event, the function re.search
is, I think, exactly what you're looking for. It takes, as its first argument, a pattern to match, and, as its second argument, the string to match it in. If the pattern is matched, search
returns an SRE_Match
object, which, conveniently, has a #start()
method that returns the index at which the match starts.
要使用示例中的数据,请执行以下操作:
To use the data from your example:
import re
start_index = re.search(r'x.z', 'xxxxxgzg').start()
请注意,在正则表达式中,.
-不是*
-是通配符,因此您必须按照使用的模式替换它们.
Note that, in regexes, .
- not *
-- is the wildcard, so you'll have to replace them in the pattern you're using.
这篇关于Python中的通配符匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!