Nathan McKaskle
Nathan McKaskle

Reputation: 3083

Python Regex: AttributeError: 'str' object has no attribute 'Match'

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)

Upvotes: 0

Views: 5897

Answers (1)

The fourth bird
The fourth bird

Reputation: 163557

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

Upvotes: 1

Related Questions