Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
issue in using regex
#1
hi
in below code( code belongs to a site that its address is in beginning of the code):
# from:https://www.geeksforgeeks.org/regular-expressions
# -python-set-1-search-match-find/


# A Python program to demonstrate working 
# of re.match(). 
import re 
	
# a sample function that uses regular expressions 
# to find month and day of a date. 
def findMonthAndDate(string): 
		
	regex = r"([a-zA-Z]+) (\d+)"
	match = re.match(regex, string) 
		
	if match == None: 
		print ("Not a valid date") 
		return
	
	print ("Given Data: %s" % (match.group())) 
	print ("Month: %s" % (match.group(1))) 
	print ("Day: %s" % (match.group(2))) 
	
		
# Driver Code 
findMonthAndDate("Jun 24") 
print("") 
findMonthAndDate("I was born on June 24")
why the result of the match in the second call of findMonthAndDate is None? the statement June 24 is in two calls, and I think it matches to defined pattern in the function. if I think wrong, how can the pattern be changed to print June 24 for the second call of the function?
thanks
Reply
#2
.match() will look at the beginning of the string.
you want to use .search() instead
relevant docs
search() vs match()


# A Python program to demonstrate working 
# of re.match(). 
import re 
     
# a sample function that uses regular expressions 
# to find month and day of a date. 
def findMonthAndDate(string): 
         
    regex = r"([a-zA-Z]+) (\d+)"
    match = re.search(regex, string) 
         
    if not match: # if match is None: 
        print ("Not a valid date") 
        return
     
    print ("Given Data: %s" % (match.group())) 
    print ("Month: %s" % (match.group(1))) 
    print ("Day: %s" % (match.group(2))) 
Output:
Given Data: Jun 24 Month: Jun Day: 24 Given Data: June 24 Month: June Day: 24
Also, you can use regex101 to play with/test regex paterns
akbarza likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
You really need to start reading about these things. You can read about re here.

https://docs.python.org/3/library/re.html

The difference between match and search is described here:

https://docs.python.org/3/library/re.htm...h-vs-match
akbarza likes this post
Reply
#4
hi
thanks for reply
can I ask what is the role of "r" in
regex = r"([a-zA-Z]+) (\d+)"
?
Reply
#5
From the docs:
Quote:Both string and bytes literals may optionally be prefixed with a letter 'r' or 'R'; such strings are called raw strings and treat backslashes as literal characters. As a result, in string literals, '\U' and '\u' escapes in raw strings are not treated specially. Given that Python 2.x’s raw unicode literals behave differently than Python 3.x’s the 'ur' syntax is not supported.
akbarza likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Facing issue in python regex newline match Shr 6 1,327 Oct-25-2023, 09:42 AM
Last Post: Shr
  Regex Issue from Automate the Boring Stuff book robgraves 2 2,835 Jun-16-2019, 12:41 AM
Last Post: robgraves

Forum Jump:

User Panel Messages

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