Date regex python

2024/10/15 15:26:36

I am trying to match dates in a string where the date is formatted as (month dd, yyyy). I am confused by what I see when I use my regex pattern below. It only matches strings that begin with a date. What am I missing?

 >>> p = re.compile('[A-z]{3}\s{1,}\d{1,2}[,]\s{1,}\d{4}')>>> s = "xyz Dec 31, 2013 - Jan 4, 2014">>> print p.match(s).start()Traceback (most recent call last):File "<stdin>", line 1, in <module>AttributeError: 'NoneType' object has no attribute 'start'>>> s = "Dec 31, 2013 - Jan 4, 2014">>> print p.match(s).start()0 #Correct
Answer

Use re.findall rather than re.match, it will return to you list of all matches:

>>> s = "Dec 31, 2013 - Jan 4, 2014"
>>> r = re.findall(r'[A-z]{3}\s{1,}\d{1,2}[,]\s{1,}\d{4}',s)
>>> r
['Dec 31, 2013', 'Jan 4, 2014']
>>>
>>> s = 'xyz Dec 31, 2013 - Jan 4, 2014'
>>> r = re.findall(r'[A-z]{3}\s{1,}\d{1,2}[,]\s{1,}\d{4}',s)
>>> r
['Dec 31, 2013', 'Jan 4, 2014']

From Python docs:

re.match(pattern, string, flags=0) If zero or more characters at thebeginning of string match the regular expression pattern, return acorresponding MatchObject instance

In the other hand:

findall() matches all occurrences of a pattern, not just the first oneas search() does.

https://en.xdnf.cn/q/117817.html

Related Q&A

Not able to install the python-module numpy

After hours of trying Im still not able to install numpy. I READ LOTS OF HINTS, ANSWERS USW. BUT IT DOESNT HELP. Furthermore I have windows 7, 32 bit, Python 27. What I did:download numpy-1.10.2.zi…

Windows Error: 32 when trying to rename file in python

Im trying to rename some PDF files using pyPdf and my code it seems to work fine until it reaches the rename sentence. The While/if block of code looks for the page number where string "This stri…

I dont quite understand the while loop in python

def AddSingleCard(self):symbols = [heart, diamond, club, spade]#newCardSign = newCardNumber, newCardSign = raw_input().split()try:newCardNumber = int(float(newCardNumber))except:newCardNumber, newCardS…

Adding a FileField to a custom SignupForm with django-allauth

I have the following custom SignupForm (simplified, works perfectly without my_file):class SignupForm(forms.Form):home_phone = forms.CharField(validators=[phone_regex], max_length=15)my_file = forms.Fi…

Checkbox to determine if an action is completed or not

I have a list of dictionaries of clients in a format like this:dict_list = [{Name of Business : Amazon, Contact Name : Jeff Bezos, Email : [email protected]}, {Name of Business : Microsoft, Contact Nam…

Python not concatenating string and unicode to link

When I append a Unicode string to the end of str, I can not click on the URL.Bad:base_url = https://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=xml&tit…

Decrypt python/django password with Symfony 2.5 (using symfony security)

I want to use symfony 2.5.10 security in order to login in from users that were created with pyhton/django security. Passwords in db that are encrypted in this format:pbkdf2_sha256$12000$dVPTWPll8poG$3…

Reuse getCmd object in pysnmp

In the pysnmp documentation there is a getCmd class, I was wondering if it was possible to just instantiate the class once and reuse it at a later point by passing it new oids. I am not sure if the ge…

Django plugged into Apache not working like Django standalone

I have encountered a hodgepodge of errors trying to bring my django site into production with Apache. Having finally gotten mod_wsgi sorted and Apache at least seeming to be trying to load the site Im …

How to filter overlap rows in a big file in python

I am trying to filter overlap rows in a big file in python.The overlap degrees is set to 25%. In other words,the number of element of intersection between any two rows is less than 0.25 times of union …