Python Forum

Full Version: Strange output with regular expressions
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi!

I'm reading about regular expressions (regex) and experimenting a bit with them. The following little program:

import re

string1 = 'The cat in the hat sat on the flat mat.'
atRegex = re.compile(r'.at')
mo1 = atRegex.findall(string1)
print(f"The string1 '{string1}' has the following '.at' words: {mo1}\n")
# 'list' object has no attribute 'group'
produces this strange output:
Output:
The string1 'The cat in the hat sat on the flat mat.' has the following '.at' words: ['cat', 'hat', 'sat', 'lat', 'mat']
and I mean strange, because instead of the word 'flat', it returns the string 'lat'. I have rewritten it, change the 'f' of 'flat' into an 'F' (Flat), but still it keeps returning 'lat' instead of 'Flat'. I don't know what I'm doing wrong, or if there is something wrong with my keyboard ...

Please, could you run it and tell me if it produces the same output on your computer or if I'm doing something wrong?

Thanks and all the best,


FOUND OUT WHAT WAS GOING ON!!!
Sorry. The answer was on the next page: the dot character will match just one character, which is why the match for the text 'flat' in the previous example matched only 'lat'.

Thank you!
(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,