saravanan s
saravanan s

Reputation: 185

Find last group of numbers in regex

I would like to identify year in sentence of last, how to find in Regex?

for ex:

string = some of texts 1999 some of texts 1986 some of text;

string = Regex.Replace(string, @"(\b(1[7-9]|2[0-9])\d{2}\b)", "<Year>$1</Year>");

my excepted output:

some of texts 1999 some of texts <year>1986</year>

Note: year will be more than one time Thanks, Saran

Upvotes: 1

Views: 183

Answers (2)

alpha bravo
alpha bravo

Reputation: 7948

or use this pattern

(.*)(\b(?:1[7-9]|2[0-9])\d{2}\b)  

and replace w/ $1<Year>$2</Year>
Demo

Upvotes: 2

vks
vks

Reputation: 67968

\b(\d+)\b(?=[^\d]*$)

or

\b((?:1[7-9]|2[0-9])\d{2})\b(?=[^\d]*$)

Try this.Replace by <year>$1</year>.See demo.

http://regex101.com/r/oE6jJ1/20

Edit

\b((?:1[7-9]|2[0-9])\d{2})\b(?!.*\b((?:1[7-9]|2[0-9])\d{2})\b)

Use this if there are numbers ahead.See demo.

http://regex101.com/r/oE6jJ1/21

Upvotes: 0

Related Questions