[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Regex extracting certain characters

Clement Ow

6/24/2008 7:39:00 AM

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?
--
Posted via http://www.ruby-....

6 Answers

Daniel Brumbaugh Keeney

6/24/2008 7:45:00 AM

0

On Tue, Jun 24, 2008 at 2:39 AM, Clement Ow
<clement.ow@asia.bnpparibas.com> 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 Keeney

Stefano Crocco

6/24/2008 7:46:00 AM

0

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

Stefano Crocco

6/24/2008 7:53:00 AM

0

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


Peña, Botp

6/24/2008 8:23:00 AM

0

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

Clement Ow

6/25/2008 2:05:00 AM

0

Peña, Botp wrote:
> From: clement.ow@asia.bnpparibas.com
> # 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.

--
Posted via http://www.ruby-....

Clement Ow

6/25/2008 5:00:00 AM

0

Peña, Botp wrote:
> From: Clement Ow [mailto:clement.ow@asia.bnpparibas.com]
> # 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! =)
--
Posted via http://www.ruby-....