• Register
1 vote
24 views

Problem:

I was trying to validate date and time using regex in python so I wrote this code:

import pandas as pd

import re

 

df = pd.read_csv('path of csv file')

coorpus = []

for i in range(0,df.shape[0]):

    x = df.iloc[i]['OrderTime']

    if re.search(r'\d{2}/\d{2}/\d{4}',x):  ### for mm/dd/yyyy

        y= re.findall(r'\d{2}/\d{2}/\d{4}',x)

        coorpus.append(y)

    elif re.search(r'\d{1}/\d{1}/\d{4}',x): ### for m/d/yyyy

        y= re.findall(r'\d{1}/\d{1}/\d{4}',x)

        coorpus.append(y)

    elif re.search(r'\d{1}/\d{2}\d{4}',x): ### for m/dd/yyyy

        y= re.findall(r'\d{1}/\d{2}/\d{4}',x)

        coorpus.append(y)

    elif re.search(r'\d{2}/\d{1}\d{4}',x): ### for mm/d/yyyy

        y= re.findall(r'\d{2}/\d{1}/\d{4}',x)

        coorpus.append(y)

    else:                     ### empty cells or says nan

        y=["00/00/0000"]

        coorpus.append(y)

 

print(coorpus)

My program consits of coloumn that consits of string that contains date and time but I am not getting the actual result. I need the regex for m/dd/yyyy and mm/d/yyyy. Can anyone help?

10 7 2
6,060 points

Please log in or register to answer this question.

1 Answer

1 vote
 
Best answer

Answer:

You can get the exact result by cahnging the orders of the regex and put the specific ones first.Your regex is not using any anchors thats why it may show m/d/yyyy in place of mm/d/yyyy.

You can also write \d{1} as \d and your code should be like :

 

import pandas as pd

import re

 

df = pd.read_csv('path of csv file')

corpus = []

for i in range(0,df.shape[0]):

    x = df.iloc[i]['OrderTime']

    if re.search(r'\d{2}/\d{2}/\d{4}',x):  ### for mm/dd/yyyy

        y= re.findall(r'\d{2}/\d{2}/\d{4}',x)

        corpus.append(y)

    elif re.search(r'\d{2}/\d/\d{4}',x): ### for mm/d/yyyy

        y= re.findall(r'\d{2}/\d/\d{4}',x)

        corpus.append(y)

    elif re.search(r'\d/\d{2}/\d{4}',x): ### for m/dd/yyyy

        y= re.findall(r'\d/\d{2}/\d{4}',x)

        corpus.append(y)

    elif re.search(r'\d/\d/\d{4}',x): ### for m/d/yyyy

        y= re.findall(r'\d/\d/\d{4}',x)

        corpus.append(y)

    else:                     ### empty cells or says nan

        y=["00/00/0000"]

        corpus.append(y)

 

print(corpus)

 

11 6 4
34,950 points

Related questions

0 votes
1 answer 45 views
45 views
My programme: import pandas as pd import re df = pd.read_csv('path of csv file') corpus = [] for i in range(0,df.shape[0]): x = df.iloc[i]['OrderTime'] if re.search(r'\d{2}/\d{2}/\d{4}',x): ### for mm/dd/yyyy y= re.findall(r'\d{2} ... programme consits of coloumn that consits of string that contains date and time but I am not getting the actual result. I need the regex for m/dd/yyyy and mm/d/yyyy.
asked Mar 8, 2020 sstones 830 points
1 vote
1 answer 15 views
15 views
Problem: I tried to get the output for the format date mm/dd/yyyy, So I wrote this program: import pandas as pd import re df = pd.read_csv('path of csv file') coorpus = [] for i in range(0,df.shape[0]): x = df.iloc[i]['OrderTime'] if re.search(r'\d{2}/\d ... append(y) else: y=["00/00/0000"] coorpus.append(y) print(coorpus) But I did not get the actual result. Can anyone help me to get this format?
asked May 12, 2020 samhaz 5k points
1 vote
1 answer 30 views
30 views
I am new to this. I just want to know how to do that?
asked Nov 3, 2020 TeamScript 13.5k points
0 votes
1 answer 7 views
7 views asked Jan 27 Afi khan 1.4k points
0 votes
1 answer 13 views
13 views
I am new to this. I was doing that but I got into some problems. I was working on that but I got into some problems.
asked Dec 3, 2020 TeamScript 13.5k points
0 votes
1 answer 7 views
7 views asked Jan 27 Afi khan 1.4k points
0 votes
1 answer 7 views
7 views asked Jan 24 waji 1.9k points
0 votes
1 answer 20 views
20 views
I was working on that but I got into some problems.
asked Nov 3, 2020 TeamScript 13.5k points
0 votes
1 answer 89 views
89 views
I'm working on a code where I will input date in DD/MM/YYYY format for example 12/04/2020. And the output should show me the date like a weekday and March 12 2020 or something like that
asked Nov 3, 2020 TeamScript 13.5k points
0 votes
0 answers 28 views
28 views
Problem: I want the date to be displayed in the date month year format on my page using HTML only.
asked Oct 13, 2020 Ahsan 1.8k points