1

I must use regex in order to get the last two digits of a year but only when 4 digits exist. I have the following regex which works perfectly when there is 4 digits. Example 2014 - 14

^.{2}

However I need this to only work when 4 digits are present. I'm having an issue with it emptying my string when only 2 digits exist.

Code Junkie
  • 7,602
  • 26
  • 79
  • 141
  • 1
    Try `..$` Looks to be a duplicate of this: http://stackoverflow.com/questions/2511588/regular-expression-extract-last-2-character"s – Hunter Brown Sep 01 '14 at 16:26
  • @HunterBrown can you use that with a not? I'm finding my pattern is replacing the last two now rather than keeping them. Thanks. – Code Junkie Sep 01 '14 at 16:32

3 Answers3

8

Simply match the four digits and capture only the last two.

^\d{2}(\d{2})$

Then reference capturing group #1 to access your match result.

hwnd
  • 69,796
  • 4
  • 95
  • 132
  • wow... I don't understand why I complicated myself with something so easy as you did lol. +1 since this is the simpliest answer. Btw, I think you should add `^` at the beginning since yours can match 123456 – Federico Piazza Sep 01 '14 at 16:38
  • So quick question, I just realized my pattern is matching on the last two which is correct, but then it replaces them. I really need to keep the last two at all times. Any thoughts. – Code Junkie Sep 01 '14 at 16:41
  • Java, I have this. @Parameter(name = "pattern", value = "(\\d{2})\\d{2}$"), @Parameter(name = "replacement", value = ""), @Parameter(name = "replace", value = "all")}), – Code Junkie Sep 01 '14 at 16:47
  • This is what I'm using http://lucene.apache.org/core/4_0_0/analyzers-common/org/apache/lucene/analysis/pattern/PatternReplaceFilterFactory.html I'm just trying to add the last two digits of a year to my index. – Code Junkie Sep 01 '14 at 16:49
  • What should I be using in the pattern? – Code Junkie Sep 01 '14 at 16:51
  • According to that link, `` – hwnd Sep 01 '14 at 16:52
  • Yay! It worked. Could you explain to me what you just did so that I understand it for the future. Thank you so much. – Code Junkie Sep 01 '14 at 16:54
  • 2
    The pattern matches from the start of the string to the end matching only 4 digits and captures only the last two. `$1` references what was captured in the grouping `( )` to put in the replacement. – hwnd Sep 01 '14 at 16:56
1

You can use this regex.

^(?(?=\d{4}$)..(\d{2}))$

Working demo

This regex uses an IF clause, so if the string is 4 digits then captures the last two.

enter image description here

Federico Piazza
  • 30,085
  • 15
  • 87
  • 123
1

The regex you have there shouldn't be working with 4 digits either. Your regex is looking for any 2 characters at the beginning of the string.

Try this:

(?<=\d\d)\d\d$

Regular expression visualization

Debuggex Demo

This is different from Fede's answer in that you don't need to use and subsequently refer to a capturing group later. Only the last 2 digits are part of the match. It relies on a positive lookbehind.

briantist
  • 45,546
  • 6
  • 82
  • 127