From: Stefano Crocco Date: 2008-06-24T16:46:00+09:00 Subject: Re: Regex extracting certain characters On Tuesday 24 June 2008, Clement Ow wrote: > Hi everyone, > > I have 2 path names : > > C:/Ruby/20080205/2008 > C:/Ruby/2008 > > I need a regular expression which can select just 2008 and utilise the > string that contains just 2008 instead of 20080205. > > I tried using /2008/ but apparently it also picks up strings like > 20080205. > is there a way to rearrange the regexp such that only 2008 is selected? If the part you're interested with is at the end of the string, you can use /\d{4}\Z/ which matches four digits followed by the end of the string. Otherwise, you can use this: /\d{4}(?:[^\d]|\Z)/ This will match four digits followed by either a non-digit character or by the end of the string. I hope this helps Stefano