how to apply regex for m/dd/yyyy

regex for date mm/dd/yyyy in java
regular expression for date format dd-mmm-yyyy
regex for year
date regex python
regular expression for month and year
regex date range generator
regex for current date
regular expression for date format mm/dd/yyyy in c#

I have a column consisting of string which contain date and time. the date is in the format of : mm/dd/yyyy , m/d/yyyy, m/dd/yyyy and mm/d/yyyy i used regex which helps me extract mm/dd/yyyy and m/d/yyyy. I am unable to extract m/dd/yyyy and mm/d/yyyy

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{1}/\d{1}/\d{4}',x): ### for m/d/yyyy
        y= re.findall(r'\d{1}/\d{1}/\d{4}',x)
        corpus.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)
        corpus.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)
        corpus.append(y)
    else:                     ### empty cells or says nan
        y=["00/00/0000"]
        corpus.append(y)

print(corpus)

for eg - the date is 12/1/2017 the output is 2/1/2017 (issue with mm/d/yyyy) for date like 1/10/2018 output is 00/00/0000

Using str.split and str.zfill and pd.to_datetime:

df = pd.DataFrame(['12/1/2017', '2/1/2017'], columns=['OrderTime'])
df1[['a','b','c']] = df.OrderTime.str.split('/',expand=True)
df['OrderTime'] = pd.to_datetime(df1.a.str.zfill(2)+'/'+df1.b.str.zfill(2)+'/'+df1.c)

print(df['OrderTime'])

0   2017-12-01
1   2017-02-01
Name: OrderTime, dtype: datetime64[ns]

If the format need to be in '%m/%d/%Y' use strftime, but this changes the datatype to object.

df.OrderTime.dt.strftime('%m/%d/%Y')
0    12/01/2017
1    02/01/2017
Name: OrderTime, dtype: object

regular expression to check date mm/dd/yyyy which allows 1/1/2013 , regular expression to validate date in mm/dd/yyy if i give single digits in month and So if you want to match either mm/dd/yyyy or m/d/yyyy use. Therefore, to validate a date String of the format MM-DD-YYYY − Compile the above mentioned regular expression using the compile() method of the Pattern class and retrieve the Pattern object. Using the object obtained above, invoke the matcher() method by passing the required date string as a parameter and retrieve the Matcher object from this method.

The reason for those results is that you should change the order of the regexes and put the most specific ones first. Your regex does not use an anchor or a word boundary, so \d{1}/\d{1}/\d{4} will match 2/1/2017 in 12/1/2017.

Note that \d{1} can be written as \d

I think your code can be written easier, but for your current approach you could try it like this:

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)

Python test

Example: Regular Expression Matching a Valid Date, To match a date in mm/dd/yyyy format, rearrange the regular expression to ^(0[1-9]|1[012])[- /.] (0[1-9]|[12][0-9]|3[01])[- /.] (19|20)\d\d$. For dd-mm-yyyy format, use ^(0[1-9]|[12][0-9]|3[01])[- /.] Regular Expression to MM-DD-YYYY format. Character classes. any character except newline \w \d \s: word, digit, whitespace

Do you know that "repetition count" can be a range rather than a single number?

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

RegEx Date Validation for M/D/YYYY and MM/DD/YYYY, Acceptable date formats are m/d/yyyy (including two-digit days and months, e.g., 10/10/2012). Note that 99.9% of the time, this field will be used  Match dates (M/D/YY, M/D/YYY, MM/DD/YY, MM/DD/YYYY) Match html tag Checks the length of number and not starts with 0 Match IDs GC TEst validateFilename key operator sepration match a wide range of international phone number

What about using pandas built-in date parser when reading in the csv file? pd.read_csv(path, parse_dates='OrderTime')

Expressions in category: Dates and Times, to ^(0[1-9]|1[012])[- /.] (0[1-9]|[12][0-9]|3[01])[- /.] (19|20)\d\d$. For dd-mm-yyyy format, use ^(0[1-9]|[12][0-9]|3[01])[- /.] Regex tutorial — A quick cheatsheet by examples. Fields of application range from validation to parsing/replacing strings, passing through translating data to other formats and web scraping

16.10 - Valid DATE Formats, You want to use a simple regex that simply checks … Solution 3: Match m/d/yy and mm/dd/yyyy, allowing any combination of one or two digits for the day and  Regex used in .NET to validate a date. Matches the following formats mm/dd/yy, mm/dd/yyyy, mm-dd-yy, mm-dd-yyyy This covers days with 30 or 31 days but does not handle February, it is allowed 30 days.

Valid Date, MM and DD could have 1 or 2 digits : M/d/yyyy or MM/d/yyyy or M/dd/yyyy This was a Source, Dany Lauener http://www.regexlib.com/REDetails.aspx?​regexp_id=1038 Please tell/provide me how can i use this in javascript,if i use this in  You want to validate dates in the traditional formats mm/dd/yy, mm/dd/yyyy, dd/mm/yy, and dd/mm/yyyy. You want to use a simple regex that simply checks whether the input looks like a date, without trying to weed out things such as February 31 st.

4.4. Validate Traditional Date Formats, MM and DD could have 1 or 2 digits : M/d/yyyy or MM/d/yyyy or NOTE: your regex engine must support lookbehinds and named groups to use this  Here I will explain how to calculate age of person from date of birth and regular expression for mm/dd/yyyy format using JavaScript. Description : In some situation we need to display age of person based on date of birth .In that situation we can calculate age of person using JavaScript by comparing present date and date of birth as shown below.