[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

confused by this simple regular expressions behaviour

Adam Akhtar

8/21/2008 8:37:00 AM

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

7 Answers

Stefano Crocco

8/21/2008 8:45:00 AM

0

On Thursday 21 August 2008, Adam Akhtar 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'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


WujcioL

8/21/2008 8:52:00 AM

0

Adam Akhtar 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) see:
http://www.rubular.com/r...
--
Posted via http://www.ruby-....

Adam Akhtar

8/21/2008 9:08:00 AM

0

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

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

Adam Akhtar

8/21/2008 9:08:00 AM

0

Thanks Mateusz as well!
--
Posted via http://www.ruby-....

Stefano Crocco

8/21/2008 9:16:00 AM

0

On Thursday 21 August 2008, Adam Akhtar 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

Peña, Botp

8/21/2008 9:21:00 AM

0

RnJvbTogQWRhbSBBa2h0YXIgW21haWx0bzphZGFtdGVtcG9yYXJ5QGdtYWlsLmNvbV0gDQojIGlz
bnQgdGhlcmUgYSBzaG9ydGhhbmQgdmVyc2lvbiBmb3IganVzdCBsZXR0ZXJzIG9yIGRvIHlvdSAN
CiMgaGF2ZSB0byBhbHdheXMgd3JpdGUgb3V0ICBbYS16QS1aXT8/Pw0KDQp0cnkNCg0KICAgL1th
LXpdL2kNCg0Kb3INCg0KICAvW1s6YWxwaGE6XV0vDQoNCg0KDQpraW5kIHJlZ2FyZHMgLWJvdHAN
Cg==

David A. Black

8/21/2008 11:02:00 AM

0

Hi --

On Thu, 21 Aug 2008, Stefano Crocco wrote:

> On Thursday 21 August 2008, Adam Akhtar 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'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

--
Rails training from David A. Black and Ruby Power and Light:
Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL
Advancing with Rails January 19-22 Fort Lauderdale, FL
See http://www.r... for details and updates!