Python Forum

Full Version: problem in matching in regex
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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