Regex extracting certain characters

C

Clement Ow

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?
 
D

Daniel Brumbaugh Keeney

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 Keeney
 
S

Stefano Crocco

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
 
S

Stefano Crocco

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
 
P

Peña, Botp

RnJvbTogY2xlbWVudC5vd0Bhc2lhLmJucHBhcmliYXMuY29tIA0KIyBDOi9SdWJ5LzIwMDgwMjA1
LzIwMDgNCiMgQzovUnVieS8yMDA4DQojIEkgbmVlZCBhIHJlZ3VsYXIgZXhwcmVzc2lvbiB3aGlj
aCBjYW4gc2VsZWN0IGp1c3QgMjAwOCBhbmQgdXRpbGlzZSB0aGUNCiMgc3RyaW5nIHRoYXQgY29u
dGFpbnMganVzdCAyMDA4IGluc3RlYWQgb2YgMjAwODAyMDUuDQoNCnBhcmRvbiBpZiBpJ20gbWlz
dGFrZW4sIGJ1dCBtYXliZSB5b3UgbWF5IHdhbnQgdG8gZ2V0IHRoZSAiZW5kaW5nIiBwYXJ0IG9m
IHRoYXQgcGF0aG5hbWUgdy9vIHVzaW5nIHJlZ2V4LA0KDQplZywNCg0Kcw0KIz0+ICJDOi9SdWJ5
LzIwMDgwMjA1LzIwMDgiDQoNCnllYXI9cy5zcGxpdCgiLyIpLmxhc3QNCiM9PiAiMjAwOCINCg0K
eWVhciA9PSAiMjAwOSINCiM9PiBmYWxzZQ0KDQp5ZWFyID09ICIyMDA4Ig0KIz0+IHRydWUNCg0K
a2luZCByZWdhcmRzIC1ib3RwDQo=
 
C

Clement Ow

From: (e-mail address removed)
# 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.
 
C

Clement Ow

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! =)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top