-2

I am trying to match the last occurrence of a persons job end year in Java.

June 1995 – June 2003 (8 years 1 month) would return 2003

1989 – June 1995 (6 years) would return 1995

The information is always in this format so I figured if I start from the end of the string and look for the first 4 digits but I not sure how to do this I know \b\d{4}\b matches the first 4 digits but I require the last occurrence of 4 digits.

NeptuneGamer
  • 123
  • 1
  • 12

3 Answers3

2

You can use this negative lookahead regex:

\bd{4}\b(?!.*\b\d{4}\b)

RegEx Demo

This will match 4 digits in a line when it is not followed by another 4 digits surrounded by word boundaries.

(?!.*\b\d{4}\b) is negative lookahead to assert that there is no 4 digit number ahead of current position in a line.

In Java you can use:

String re = "\\bd{4}\\b(?!.*\\b\\d{4}\\b)";
anubhava
  • 761,203
  • 64
  • 569
  • 643
1

Match the lot then grab the second year:

[A-Za-z]+\\s\\d{4}\\s–\\s[A-Za-z]+\\s(?<year>\\d{4}).*

Then just get the value with matcher.group("year").

Boris the Spider
  • 59,842
  • 6
  • 106
  • 166
1

In order to get the last occurrence of a pattern, just prepend it with a greedy .*. Then to extract the desired pattern either use \K:

.*\K\b\d{4}\b

(not supported in Java, unfortunately), or wrap your pattern in a capture group:

.*(\b\d{4}\b)

and access the group in your code:

String input = "June 1995 – June 2003 (8 years 1 month)";
Pattern pattern = Pattern.compile(".*(\\b\\d{4}\\b)");
Matcher matcher = pattern.matcher(input);
if (matcher.find()) {
    System.out.print(matcher.group(1));
}

Demo: https://ideone.com/cMAvFH

Dmitry Egorov
  • 9,542
  • 3
  • 22
  • 40