# Confused by this simple regular expressions behaviour

**URL:** <https://www.ruby-forum.com/t/confused-by-this-simple-regular-expressions-behaviour/146452>\
**Category:** Ruby\
**Created:** [August 21, 2008, 10:40am UTC](https://www.ruby-forum.com/t/confused-by-this-simple-regular-expressions-behaviour/146452 "2008-08-21T10:40:35Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [August 21, 2008, 10:40am UTC](https://www.ruby-forum.com/t/confused-by-this-simple-regular-expressions-behaviour/146452/1 "2008-08-21T10:40:35Z")

</div>

why does this regexpression match

\w+\d+

match 68 in this

“1/8 Fender '68 TELECASTER Miniature(Pink Paisley)[RARE]”,

ive told it to at least match one letter before it matches any  
numbers???

(i dont actually want it to match anything with this sentance).

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [August 21, 2008, 10:53am UTC](https://www.ruby-forum.com/t/confused-by-this-simple-regular-expressions-behaviour/146452/2 "2008-08-21T10:53:08Z")

</div>

On Thursday 21 August 2008, Adam A. wrote:

> (i dont actually want it to match anything with this sentance).

It’s because \w stands for [a-zA-Z0-9], that is an uppercase letter or a  
lowercase letter or a digit. To achieve what you want you need this:

/[a-zA-Z]+\d+/

Stefano

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [August 21, 2008, 10:55am UTC](https://www.ruby-forum.com/t/confused-by-this-simple-regular-expressions-behaviour/146452/3 "2008-08-21T10:55:14Z")

</div>

Adam A. wrote:

> why does this regexpression match
> 
> \w+\d+
> 
> match 68 in this
> 
> “1/8 Fender '68 TELECASTER Miniature(Pink Paisley)[RARE]”,
> 
> ive told it to at least match one letter before it matches any  
> numbers???
> 
> (i dont actually want it to match anything with this sentance).

It matchs on rubular ([rubular.com](http://rubular.com)) see:  
[http://www.rubular.com/regexes/946](http://www.rubular.com/regexes/946)

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [August 21, 2008, 11:11am UTC](https://www.ruby-forum.com/t/confused-by-this-simple-regular-expressions-behaviour/146452/4 "2008-08-21T11:11:31Z")

</div>

hey stefano thanks for pointing that out…cant believe i missed that  
one.

isnt there a shorthand version for just letters or do you have to always  
write out [a-zA-Z]???

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [August 21, 2008, 11:20am UTC](https://www.ruby-forum.com/t/confused-by-this-simple-regular-expressions-behaviour/146452/5 "2008-08-21T11:20:06Z")

</div>

On Thursday 21 August 2008, Adam A. wrote:

> hey stefano thanks for pointing that out…cant believe i missed that  
> one.
> 
> isnt there a shorthand version for just letters or do you have to always  
> write out [a-zA-Z]???

As far as I know, there’s not such shortcut.

Stefano

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [August 21, 2008, 11:12am UTC](https://www.ruby-forum.com/t/confused-by-this-simple-regular-expressions-behaviour/146452/6 "2008-08-21T11:12:11Z")

</div>

Thanks Mateusz as well!

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [August 21, 2008, 11:25am UTC](https://www.ruby-forum.com/t/confused-by-this-simple-regular-expressions-behaviour/146452/7 "2008-08-21T11:25:57Z")

</div>

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

# isnt there a shorthand version for just letters or do you

# have to always write out [a-zA-Z]???

try

/[a-z]/i

or

/[[:alpha:]]/

kind regards -botp

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [August 21, 2008, 1:06pm UTC](https://www.ruby-forum.com/t/confused-by-this-simple-regular-expressions-behaviour/146452/8 "2008-08-21T13:06:29Z")

</div>

Hi –

On Thu, 21 Aug 2008, Stefano C. wrote:

> > numbers???
> > 
> > (i dont actually want it to match anything with this sentance).
> 
> It’s because \w stands for [a-zA-Z0-9], that is an uppercase letter or a  
> lowercase letter or a digit.

And also underscore.

David
