Regexp to split month from year in date mmyyyy

regex date yyyymmdd
regular expression for month and year
regex for all date formats
regular expression for date format mm/dd/yyyy or m/d/yyyy
regex for date mm/dd/yyyy in java
regex month name
regular expression for date format dd-mmm-yyyy
regular expression for date format dd/mm/yyyy in javascript

I have a variable which is the concatenation of month and year, in a numeric format. The month is in format 1-12, not 01-12.

My variable looks like:

mmyyyy
12014
22014
102014
52015
112015

I am looking for a regexp to match the month or the year only:

for year, I did something like:

year <- ifelse(grepl("2014", mmyyyy), 2014, ifelse(grepl("2015", mmyyyy), 2015, 2016))

But for the month, I am struggling. My first thought is to replace 2014, 2015, etc. by blank then to convert the result in numeric.

month <- as.numeric(gsub("[[^2014]]", "", mmyyyy))

but here, I can't find a suitable regexp expression.

In the end, I would like a variable/ vector with the numeric year(yyyy) and a variable/vector with the numeric month.


One option is

# for the months:
> as.numeric(gsub("(.*)[0-9]{4}$", "\\1", x))
#[1]  1  2 10  5 11
# for the years:
> as.numeric(gsub(".*([0-9]{4})$", "\\1", x))
#[1] 2014 2014 2014 2015 2015

This works for any 4-digit years.

Example: Regular Expression Matching a Valid Date, Example of a regular expression to find dates or to check if the user entered a valid date. To be really perfectionist, you would have to split up the month into various options to For example, excluding February 29th when the year is not a leap year is far easier to For dd-mm-yyyy format, use ^(0[1-9]|[12][0-9]|3[01])[- /.]​  Here is a simple credit card expiration date regular expression. I wrote my own regexp for the expiration date since one was not easily found on the net. This one validates for allowing the year 2009 to 2029, but could be easily changed…I built this for an ASP.NET programming project for a course I’m taking.


A possible solution using tidyr Which will create both month and year columns simultaneously in one call.

library(tidyr)
extract(df, mmyyyy, c("month", "year"), "(\\d+)(\\d{4})", convert = TRUE)
#   month year
# 1     1 2014
# 2     2 2014
# 3    10 2014
# 4     5 2015
# 5    11 2015

Data

df <- data.frame(mmyyyy = c(12014,
                            22014,
                            102014,
                            52015,
                            112015))

regular expression, to use regular expression to extract the month from a date of DD/MM/YYYY. The easiest way to go about this is separate the day, month, and year into 3  I have a variable which is the concatenation of month and year, in a numeric format. The month is in format 1-12, not 01-12. My variable looks like: mmyyyy 12014 22014 102014 52015 112015 I am l


mmyyyy <- c(12014,22014,102014, 52015, 112015)

Making a 6 digit vector using sprintf

dates <- sprintf("%06d", mmyyyy)

You could use the yearmon function from the zoo package

library(zoo)
dates1 <- as.yearmon(dates, format = "%m%Y")
format(dates1, "%m")
# [1] "01" "02" "10" "05" "11"
format(dates1, "%Y")
# [1] "2014" "2014" "2014" "2015" "2015"

EDIT: Updated as per @David's comments

Ivor Horton's Beginning Java, System.out.println("Enter your birth date as dd mm yyyy: "); try { day = in. today.​get(YEAR), birthdate.get(MONTH), birthdate.get(DATE)); int age REGULAR EXPRESSIONS You saw some elementary capability for searching strings Dates​  Regular Expression to Allows dates following these rules: - days may have leading zeros. 1-31. max 2 digits - months may have leading zeros. 1-12. max 2 digits - years 1900-2099. 4 digits


You could use the package unglue :

df <- data.frame(mmyyyy = c(12014, 22014, 102014, 52015, 112015))
library(unglue)
unglue_unnest(df, mmyyyy, "{month}{year=\\d{4}}", convert = TRUE)
#>   month year
#> 1     1 2014
#> 2     2 2014
#> 3    10 2014
#> 4     5 2015
#> 5    11 2015

Ivor Horton's Beginning Java 2, Create birth date calendar – month is 0 to 11 GregorianCalendar birthdate today.get(YEAR), birthdate.get(MONTH), birthdate.get(DATE)); int age = today.​get(today. I got the following output: Enter your birth date as dd mm yyyy: 5 Regular Expressions You saw some elementary capability for searching strings 690  4.4. Validate Traditional Date Formats Problem 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 … - Selection from Regular Expressions Cookbook, 2nd Edition [Book]


How about something like below (assuming you are only dealing with years > 2000)

month <- as.numeric(gsub("20[0-9]+", "", mmyy))

Date Validation in Javascript. Example using split and Regex for dd , In the first method, we are splitting the dd, mm, yyyy using substring method. isDigit () funtion is used to validate the dd, mm, yyyy is numeric or not and we have written logic to check the day, month, year is in the valid range. In the Second method, Date is split using Regular expression. I have string variable containing date from which I would like to extract only the day. Date format: dd/mm/yyyy. DateTime date_var= new DateTime(2017, 10, 13); Console.WriteLine(date_var.Day);


Regular expression to extract date(YYYY-MM-DD) from string , //Regular expression to extract date(YYYY-MM-DD) from string package main import ( "fmt" "regexp" ) func main() { str1 := "If I am 20 years 10 months and 14  Java Regular Expression Validate date example shows how to validate date using regex in Java. Example also shows how to validate date using regex for yyyy-mm-dd, yyyy/mm/dd, dd/mm/yyyy, mm/dd/yyyy and other formats.


PHP Object-Oriented Solutions, 15 magic quotes, 15 OOP features in, 13 Perl-compatible regular expressions DD/MM/YYYY format, 98–99 declaring the year, month, and date as protected date in a predefined format, 114 format(), 92, 100–102 __get(), 115 getDMY(),​  If the year format is in yyyy form you can change your expression to [Month]+", "+ [Year] (note the space after the comma) and then use the DateTime tool to format your string into a date. The DateTime tool can convert "Month, yyyy" format into a Date.


4.4. Validate Traditional Date Formats, Selection from Regular Expressions Cookbook, 2nd Edition [Book] dates in the traditional formats mm/dd/yy, mm/dd/yyyy, dd/mm/yy, and dd/mm/yyyy. of one or two digits for the day and month, and two or four digits for the year: Get Regular Expressions Cookbook, 2nd Edition now with O'Reilly online learning. date format (yyyy-mm-dd) Comments. Post Posting Guidelines Formatting - Now. Top Regular Expressions. Match itnegers test number Regex Tester requires a modern