You are viewing limited content. For full access, please sign in.

Question

Question

pattern matching the year out of a date field

asked on February 25, 2015

I know this is simple I just can't remember the expression to use. I am looking to extract the year out of the date field that is 2/21/2014. Sometimes it will be one digit for the month and sometimes two digits. I can't remember for the life of me what the expression is to let the pattern no it doesn't care if it is one or two numbers at the beginning

0 0

Replies

replied on February 25, 2015

Why not use date formatting instead? If you want to use pattern matching, assuming the year is the last set of digits in the date, something like (\d{4})$ should do it.

0 0
replied on February 25, 2015

if the date is a Date field you can simply use yyyy and it will return only the year of a date field.

 

 

0 0
replied on February 25, 2015 Show version history

James, I also recommend using date formatting as Miruna suggested. Her regex should work as well. Now, if you cannot guarantee that the input is the end of the text (such as if you are getting this date from a large amount of text) then you can use the following regex as well:

 

\d{1,2}/\d{1,2}/(\d{4})

This says "one to two digits, then a slash, then one to two digits, then a slash, then capture 4 digits".

 

Alternatively you can use the "?" operator for the same effect:

\d?\d/\d?\d/(\d{4})

This says "one digit (optional), then one digit, then a slash, then one digit (optional), then one digit, then a slash, then capture 4 digits.

 

Note that in both cases, this will accept an input like 99/99/2014, so you may prefer to use date formatting unless you wanna restrict your regex by doing something like 

[01]?[1-9]/[0-3]?[1-9]/(\d{4}) 

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.