Python Forum
problem in matching in regex
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
problem in matching in regex
#1
hi
the below code was created by and copied from the site: regex101.com and I changed some of it:
# This code is copied from 'regex101.com' and then has been changed some.

''' The aim of this code is to find lines in  a multiline
    text that each line begins with the word "The" .
'''    

import re

regex = r"[\A|\^]The\w+'"

test_str = "These books are good.\nThe weather is nice.\nwhere are you going?\n"
print(test_str)


matches = re.finditer(regex, test_str, re.MULTILINE)

for matchNum, match in enumerate(matches, start=1):
    
    print ("Match {matchNum} was found at {start}-{end}: {match}".
           format(matchNum = matchNum, start = match.start(),
                  end = match.end(), match = match.group()))
    
    for groupNum in range(0, len(match.groups())):
        groupNum = groupNum + 1
        
        print ("Group {groupNum} found at {start}-{end}: {group}".
               format(groupNum = groupNum, start = match.start(groupNum),
              end = match.end(groupNum), group = match.group(groupNum)))

# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
the aim of this code is to determine the lines that begin with the word The in a multiline text.
note: I wrote regex myself.
when I ran it, an error message appeared (the error message was long, so I did not import it here.)
what is wrong? Is written regex with me true?
I want to ask another question:
in the site regex101.com with r"Rain\Z" regex, I inputed the Rain string fot test matching and then i clicked enter. but it showed no match(5step, 0.0ms)
also for string the cold and Rain with the same regex, it showed no match. why? I think because the word Rain is at the end of these phrases, it must be a match.
thanks
Reply
#2
The regex is not at all right for this task,as you use regex101 on right side it will explain what the regex is doing.
import re

test_str = "These books are good.\nThe weather is nice.\nwhere are you going?\nthe car is green"
pattern = r'^The\s.*'
matches = re.finditer(pattern, test_str, re.MULTILINE)
for match in matches:
    print(match.group()) 
Output:
The weather is nice.
If also want lowercase add re.MULTILINE | re.IGNORECASE
Output:
The weather is nice. the car is green
akbarza likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem with code / audio is playing randomly, not matching csv requirements Daniel_kcr 2 770 Sep-07-2023, 05:09 PM
Last Post: deanhystad
  Problem in Regex Expression shantanu97 2 1,842 Sep-28-2021, 03:40 AM
Last Post: shantanu97
  Matching Regex in Python from Excelfile and inserting into database Shuzo 0 1,713 May-14-2020, 06:53 PM
Last Post: Shuzo
  Automate the boring stuff: regex not matching DJ_Qu 7 3,980 Apr-27-2019, 07:03 AM
Last Post: micseydel

Forum Jump:

User Panel Messages

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