Reputation: 893
I am trying to find a way to match a pattern p in a string s in python.
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.
Upvotes: 13
Views: 93632
Reputation: 844
Compile a Python regular expressions object for some pattern of interest and then pass the string to its Match(string) method. You'll want to use a match
object if you need a boolean output: https://docs.python.org/3/library/re.html#match-objects
Example: Check string s for any word character (that is, alphanumerics)
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!
Upvotes: 0
Reputation: 28596
I convert your pattern into a regular expression that can then be used by 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).
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))
Upvotes: 7
Reputation: 840
If I'm understanding your question, you're looking for a pythonic approach to pattern matching across a set of strings.
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
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']
Upvotes: 1
Reputation: 2505
You could make use of regular expressions.
Have a look here for some examples: Link
I think you could use re.search()
Ecample:
import re
stringA = 'dog cat mouse'
stringB = 'cat'
# Look if stringB is in stringA
match = re.search(stringB, stringA)
if match:
print('Yes!')
Upvotes: 3