Nov-02-2019, 05:12 AM
Hi!
I'm reading about regular expressions (regex) and experimenting a bit with them. The following little program:
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!
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!