[英]Python: Find pattern in a string
I am trying to find a way to match a pattern p in a string s in python. 我试图找到一种方法来匹配python中字符串s中的模式p。
s = 'abccba'
ss = 'facebookgooglemsmsgooglefacebook'
p = 'xyzzyx'
# s, p -> a, z # s and p can only be 'a' through 'z'
def match(s, p):
if s matches p:
return True
else:
return False
match(s, p) # return True
match(ss, p) # return True
I just tried: 我刚尝试过:
import re
s = "abccba"
f = "facebookgooglemsmsgooglefacebook"
p = "xyzzyx"
def fmatch(s, p):
p = re.compile(p)
m = p.match(s)
if m:
return True
else:
return False
print fmatch(s, p)
print fmatch(f, p)
Both return false; 两者都归零; they are supposed to be true.
他们应该是真的。
I convert your pattern into a regular expression that can then be used by re.match
. 我将您的模式转换为正则表达式,然后由
re.match
。 For example, your xyzzyx
becomes (.+)(.+)(.+)\\3\\2\\1$
(the first occurrence of each letter becomes a capture group (.+)
, and subsequent occurences become the proper back reference). 例如,你的
xyzzyx
变为(.+)(.+)(.+)\\3\\2\\1$
(每个字母的第一次出现变成一个捕获组(.+)
,随后的出现成为正确的后向引用) 。
import re
s = 'abccba'
ss = 'facebookgooglemsmsgooglefacebook'
p = 'xyzzyx'
def match(s, p):
nr = {}
regex = []
for c in p:
if c not in nr:
regex.append('(.+)')
nr[c] = len(nr) + 1
else:
regex.append('\\%d' % nr[c])
return bool(re.match(''.join(regex) + '$', s))
print(match(s, p))
print(match(ss, p))
If I'm understanding your question, you're looking for a pythonic approach to pattern matching across a set of strings. 如果我理解你的问题,那么你正在寻找一种pythonic方法来对一组字符串进行模式匹配。
Here is an example demonstrating the use of list comprehensions to achieve this goal. 这是一个示例,演示了使用列表推导来实现这一目标。
I hope it helps you reach your goal. 我希望它可以帮助您实现目标。 Please let me know if I can help further.
如果我能进一步帮助,请告诉我。 - JL
- JL
Demonstrate No Match Found 证明找不到匹配
>>> import re
>>> s = ["abccba", "facebookgooglemsmsgooglefacebook"]
>>> p = "xyzzyx"
>>> result = [ re.search(p,str) for str in s ]
>>> result
[None, None]
Demonstrate Combination of Matches and No Match in the result 在结果中展示匹配和不匹配的组合
>>> p = "abc"
>>> result = [ re.search(p,str) for str in s ]
>>> result
[<_sre.SRE_Match object at 0x100470780>, None]
>>> [ m.group(0) if m is not None else 'No Match' for m in result ]
['abc', 'No Match']
>>> [ m.string if m is not None else 'No Match' for m in result ]
['abccba', 'No Match']
Demonstrate single statement 展示单一陈述
>>> [ m.string if m is not None else 'No Match' for m in [re.search(p,str) for str in s] ]
['abccba', 'No Match']
Compile a Python regular expressions object for some pattern of interest and then pass the string to its Match(string) method. 为某些感兴趣的模式编译Python正则表达式对象,然后将该字符串传递给其Match(string)方法。 You'll want to use a
match
object if you need a boolean output: https://docs.python.org/3/library/re.html#match-objects 如果需要布尔输出,则需要使用
match
对象: https : //docs.python.org/3/library/re.html#match-objects
Example: Check string s for any word character (that is, alphanumerics) 示例:检查字符串s是否包含任何单词字符(即字母数字)
def match_string(s):
##compile a regex for word characters
regex = re.compile("\\w")
##return the result of the match function on string
return re.match(s)
Hope it helps! 希望能帮助到你!
You could make use of regular expressions. 您可以使用正则表达式。
Have a look here for some examples: Link 看看这里的一些例子: 链接
I think you could use re.search()
我想你可以使用
re.search()
Ecample: Ecample:
import re
stringA = 'dog cat mouse'
stringB = 'cat'
# Look if stringB is in stringA
match = re.search(stringB, stringA)
if match:
print('Yes!')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.