owc
June 24, 2008, 9:41am
1
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?
owc
June 24, 2008, 9:48am
2
On Tue, Jun 24, 2008 at 2:39 AM, Clement Ow
[email protected] wrote:
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’m not exactly certain, but you may want /\D/2008$/
Daniel Brumbaugh K.
owc
June 24, 2008, 9:48am
3
On Tuesday 24 June 2008, Clement Ow wrote:
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
owc
June 24, 2008, 10:25am
4
From: [email protected]
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.
pardon if i’m mistaken, but maybe you may want to get the “ending” part
of that pathname w/o using regex,
eg,
s
#=> “C:/Ruby/20080205/2008”
year=s.split("/").last
#=> “2008”
year == “2009”
#=> false
year == “2008”
#=> true
kind regards -botp
owc
June 24, 2008, 9:56am
5
On Tuesday 24 June 2008, Stefano C. wrote:
/\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
owc
June 25, 2008, 4:07am
6
Peña, Botp wrote:
From: [email protected]
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.
pardon if i’m mistaken, but maybe you may want to get the “ending” part
of that pathname w/o using regex,
eg,
s
#=> “C:/Ruby/20080205/2008”
year=s.split("/").last
#=> “2008”
year == “2009”
#=> false
year == “2008”
#=> true
kind regards -botp
Thank you for all your prompt replies =)
I think I did not explain myself clearer…
What I want to test for is actually just solely ‘/2008’ which can be
anywhere in the path name, maybe citing some examples would be clearer.
These are the kind of strings i want to test for:
C:/2008/ruby or C:/ruby/test/2008 or C:/ruby/test/2008/testing
These are the kind of strings that I want to eliminate:
C:/20080229/ruby or C:/ruby/test/20080502 or
C:/ruby/test/20080405/testing or C:/29022008/ruby
In other words, I want to just test for “/” followed by “2008” in a
given string.
Thanks.
owc
June 25, 2008, 4:30am
7
From: Clement Ow [mailto:[email protected] ]
What I want to test for is actually just solely ‘/2008’ which can be
anywhere in the path name, maybe citing some examples would
be clearer.
These are the kind of strings i want to test for:
C:/2008/ruby or C:/ruby/test/2008 or C:/ruby/test/2008/testing
These are the kind of strings that I want to eliminate:
C:/20080229/ruby or C:/ruby/test/20080502 or
C:/ruby/test/20080405/testing or C:/29022008/ruby
In other words, I want to just test for “/” followed by “2008” in a
given string.
the solutions given by Stefano are great. just try them.
i can show some other ways, eg
a
#=> [“C:/2008/ruby”, “C:/ruby/test/2008”, “C:/ruby/test/2008/testing”,
“C:/20080229/ruby”, “C:/ruby/test/20080502”,
C:/ruby/test/20080405/testing", “C:/29022008/ruby”]
a.grep /(^|/)2008(/|$)/
#=> [“C:/2008/ruby”, “C:/ruby/test/2008”, “C:/ruby/test/2008/testing”]
and again, another non-regex way,
a.select{|path| path.split(“/”).any?{|x| x==“2008”}}
#=> [“C:/2008/ruby”, “C:/ruby/test/2008”, “C:/ruby/test/2008/testing”]
btw, if you have “C:/ruby/2008/20080502”, would you accept it?
it would also be nice if you could show sample code snippet of your
problem. remember, there are many ways
kind regards -botp
owc
June 25, 2008, 7:01am
8
Peña, Botp wrote:
From: Clement Ow [mailto:[email protected] ]
What I want to test for is actually just solely ‘/2008’ which can be
anywhere in the path name, maybe citing some examples would
be clearer.
These are the kind of strings i want to test for:
C:/2008/ruby or C:/ruby/test/2008 or C:/ruby/test/2008/testing
These are the kind of strings that I want to eliminate:
C:/20080229/ruby or C:/ruby/test/20080502 or
C:/ruby/test/20080405/testing or C:/29022008/ruby
In other words, I want to just test for “/” followed by “2008” in a
given string.
the solutions given by Stefano are great. just try them.
i can show some other ways, eg
a
#=> [“C:/2008/ruby”, “C:/ruby/test/2008”, “C:/ruby/test/2008/testing”,
“C:/20080229/ruby”, “C:/ruby/test/20080502”,
C:/ruby/test/20080405/testing", “C:/29022008/ruby”]
a.grep /(^|/)2008(/|$)/
#=> [“C:/2008/ruby”, “C:/ruby/test/2008”, “C:/ruby/test/2008/testing”]
and again, another non-regex way,
a.select{|path| path.split(“/”).any?{|x| x==“2008”}}
#=> [“C:/2008/ruby”, “C:/ruby/test/2008”, “C:/ruby/test/2008/testing”]
btw, if you have “C:/ruby/2008/20080502”, would you accept it?
hmmm, for this string I might want to exclude… Any ideas how it can be
done using Regexp?
it would also be nice if you could show sample code snippet of your
problem. remember, there are many ways
kind regards -botp
Oh yea, cool ways there… I used /(^|/)2008(/|$)/ in the end… Thanks
it works! =)