# Regex extracting certain characters

**URL:** <https://www.ruby-forum.com/t/regex-extracting-certain-characters/141281>\
**Category:** Ruby\
**Created:** [June 24, 2008, 9:41am UTC](https://www.ruby-forum.com/t/regex-extracting-certain-characters/141281 "2008-06-24T09:41:06Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![owc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/74df32/32.png) [@owc](https://www.ruby-forum.com/u/owc)\
**Post date:** [June 24, 2008, 9:41am UTC](https://www.ruby-forum.com/t/regex-extracting-certain-characters/141281/1 "2008-06-24T09:41:06Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![owc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/74df32/32.png) [@owc](https://www.ruby-forum.com/u/owc)\
**Post date:** [June 24, 2008, 9:48am UTC](https://www.ruby-forum.com/t/regex-extracting-certain-characters/141281/2 "2008-06-24T09:48:18Z")

</div>

On Tue, Jun 24, 2008 at 2:39 AM, Clement Ow  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![owc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/74df32/32.png) [@owc](https://www.ruby-forum.com/u/owc)\
**Post date:** [June 24, 2008, 9:48am UTC](https://www.ruby-forum.com/t/regex-extracting-certain-characters/141281/3 "2008-06-24T09:48:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![owc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/74df32/32.png) [@owc](https://www.ruby-forum.com/u/owc)\
**Post date:** [June 24, 2008, 10:25am UTC](https://www.ruby-forum.com/t/regex-extracting-certain-characters/141281/4 "2008-06-24T10:25:18Z")

</div>

From: removed\_email\_address@domain.invalid

# 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

---

<div class="post-metadata">

**Author:** ![owc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/74df32/32.png) [@owc](https://www.ruby-forum.com/u/owc)\
**Post date:** [June 24, 2008, 9:56am UTC](https://www.ruby-forum.com/t/regex-extracting-certain-characters/141281/5 "2008-06-24T09:56:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![owc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/74df32/32.png) [@owc](https://www.ruby-forum.com/u/owc)\
**Post date:** [June 25, 2008, 4:07am UTC](https://www.ruby-forum.com/t/regex-extracting-certain-characters/141281/6 "2008-06-25T04:07:13Z")

</div>

PeÃ±a, Botp wrote:

> From: removed\_email\_address@domain.invalid
> 
> # 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.

---

<div class="post-metadata">

**Author:** ![owc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/74df32/32.png) [@owc](https://www.ruby-forum.com/u/owc)\
**Post date:** [June 25, 2008, 4:30am UTC](https://www.ruby-forum.com/t/regex-extracting-certain-characters/141281/7 "2008-06-25T04:30:23Z")

</div>

From: Clement Ow [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# 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

---

<div class="post-metadata">

**Author:** ![owc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/74df32/32.png) [@owc](https://www.ruby-forum.com/u/owc)\
**Post date:** [June 25, 2008, 7:01am UTC](https://www.ruby-forum.com/t/regex-extracting-certain-characters/141281/8 "2008-06-25T07:01:47Z")

</div>

PeÃ±a, Botp wrote:

> From: Clement Ow [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]
> 
> # 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! =)
