0
votes

This is the first time I'm using regular expressions with python and I'm trying to figure out why it thinks the error "AttributeError: 'str' object has no attribute 'Match'"

I'm doing this in Jupyter notebook. What am I doing wrong here? I see only one other unhelpful question out there with this same missing attribute error.

import re
grp =  "Application: Company Name / 184010 - Application Development / 184010 - Contract Express"
rgx = "\w+ *(?!.*-)"
res = grp.match(rgx)
print(res)
1
Wrong syntax. Either re.match(rgx, grp) or re.compile(rgx).match(grp).meowgoesthedog
What are you trying to match here?Tim Biegeleisen
All words after the final dash @TimBiegeleisenNathan McKaskle
@meowgoesthedog thanks!Nathan McKaskle
@TimBiegeleisenweirdly I'm not getting a match, the result is none yet in the regex tester I used it matches fine.Nathan McKaskle

1 Answers

1
votes

You want to use re.match but that starts at the beginning of the string. You could use findall instead:

import re
grp =  "Application: Company Name / 184010 - Application Development / 184010 - Contract Express"
rgx = "\w+ *(?!.*-)"
res = re.findall(rgx, grp)
print(res)  # ['Contract ', 'Express']

Python demo

If there should also not be a forward slash following, you could add that to a character class together with the hyphen.

Note that to not match the space after the word you could omit the space followed by the asterix * from the pattern.

\w+(?!.*[-/])

Regex demo | Python demo