From: Stefano Crocco Date: 2008-06-24T16:53:26+09:00 Subject: Re: Regex extracting certain characters On Tuesday 24 June 2008, Stefano Crocco wrote: > 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 Sorry, I think I misunderstood what you wanted. You need a regexp which matches only the second string, while I suggested you two regexps which matched the 2008 part of both. A possible solution is this: /[^\d]\/2008\Z/ which matches a non-digit character, followed by a / followed by 2008 and by the end of the string. Stefano