Python Forum
Strange output with regular expressions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Strange output with regular expressions
#1
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!
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
Reply
#2
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Recursive regular expressions in Python risu252 2 1,127 Jul-25-2023, 12:59 PM
Last Post: risu252
Sad Regular Expressions - so close yet so far bigpapa 5 894 May-03-2023, 08:18 AM
Last Post: bowlofred
  Having trouble with regular expressions mikla 3 2,543 Mar-16-2021, 03:44 PM
Last Post: bowlofred
  Statements and Expressions Julie 1 1,591 Feb-26-2021, 05:19 PM
Last Post: nilamo
  Regular Expressions pprod 4 3,018 Nov-13-2020, 07:45 AM
Last Post: pprod
  Pandas's regular expression function result is so strange cools0607 6 3,097 Jun-15-2020, 07:34 AM
Last Post: cools0607
  Format phonenumbers - regular expressions Viking 2 1,858 May-11-2020, 07:27 PM
Last Post: Viking
  Strange output kintarowonders 6 2,886 May-08-2020, 05:20 PM
Last Post: buran
  regular expressions in openpyxl. format picnic 0 2,448 Mar-28-2020, 09:47 PM
Last Post: picnic
  Unexpected (?) result with regular expressions guraknugen 2 2,164 Jan-18-2020, 02:33 PM
Last Post: guraknugen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020