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


Messages In This Thread
RE: Strange output with regular expressions - by newbieAuggie2019 - Nov-04-2019, 07:06 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Use or raw string on regular expressions Zaya_pool 5 473 May-09-2024, 06:10 PM
Last Post: Zaya_pool
Information Do regular expressions still need raw strings? bobmon 3 459 May-03-2024, 09:05 AM
Last Post: rishika24
  Recursive regular expressions in Python risu252 2 1,543 Jul-25-2023, 12:59 PM
Last Post: risu252
Sad Regular Expressions - so close yet so far bigpapa 5 1,151 May-03-2023, 08:18 AM
Last Post: bowlofred
  Having trouble with regular expressions mikla 3 2,756 Mar-16-2021, 03:44 PM
Last Post: bowlofred
  Regular Expressions pprod 4 3,240 Nov-13-2020, 07:45 AM
Last Post: pprod
  Pandas's regular expression function result is so strange cools0607 6 3,357 Jun-15-2020, 07:34 AM
Last Post: cools0607
  Format phonenumbers - regular expressions Viking 2 2,022 May-11-2020, 07:27 PM
Last Post: Viking
  Strange output kintarowonders 6 3,110 May-08-2020, 05:20 PM
Last Post: buran
  regular expressions in openpyxl. format picnic 0 2,566 Mar-28-2020, 09:47 PM
Last Post: picnic

Forum Jump:

User Panel Messages

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