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
Question
Question
pattern matching the year out of a date field
Replies
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.
if the date is a Date field you can simply use yyyy and it will return only the year of a date field.
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})