Nov-04-2019, 07:06 PM
(Nov-02-2019, 05:12 AM)newbieAuggie2019 Wrote: the dot character will match just one character, which is why the match for the text 'flat' in the previous example matched only 'lat'.Hi!
Just in case some other newbies have wondered how to make the regular expressions match also 'flat' with the other words ending in '-at':
import re string1= 'The cat in the hat sat on the flat mat.' atRegex = re.compile(r'\S*at') mo1 = atRegex.findall(string1) print(f"The string1 '{string1}' has the following words ending in '-at': {mo1}\n") # 'list' object has no attribute 'group' ##\S Any character that is not a space, ##tab, or newline. ## ##The * (called the star or asterisk) ##means “match zero or more”—the group ##that precedes the star can occur any ##number of times in the text. It can ##be completely absent or repeated ##over and over again.with the following output:
Output:The string1 'The cat in the hat sat on the flat mat.' has the following words ending in '-at': ['cat', 'hat', 'sat', 'flat', 'mat']
All the best,
newbieAuggie2019
"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs