[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Is [:alnum:] equivalent to [A-Za-z0-9]?

Jeff

1/10/2007 2:33:00 PM

Hi,

I'm trying to creating a regex that will match a string that starts and
ends with an alphanumeric character, and has alphanumeric characters or
dashes in the middle.

This regex gives the expected result:
irb(main):008:0> /^[:alnum:][A-Za-z0-9\-]*[A-Za-z0-9]$/ =~ "apple!"
=> nil
irb(main):009:0> /^[:alnum:][A-Za-z0-9\-]*[A-Za-z0-9]$/ =~ "apple"
=> 0

This regex works as expected on "apple!", but returns nil on "apple":
irb(main):010:0> /^[:alnum:][A-Za-z0-9\-]*[:alnum:]$/ =~ "apple!"
=> nil
irb(main):011:0> /^[:alnum:][A-Za-z0-9\-]*[:alnum:]$/ =~ "apple"
=> nil

Can anyone explain this?

Thanks,
Jeff

3 Answers

Vincent Fourmond

1/10/2007 2:48:00 PM

0

Jeff wrote:
> Hi,
>
> I'm trying to creating a regex that will match a string that starts and
> ends with an alphanumeric character, and has alphanumeric characters or
> dashes in the middle.
>
> This regex gives the expected result:
> irb(main):008:0> /^[:alnum:][A-Za-z0-9\-]*[A-Za-z0-9]$/ =~ "apple!"
> => nil
> irb(main):009:0> /^[:alnum:][A-Za-z0-9\-]*[A-Za-z0-9]$/ =~ "apple"
> => 0
>
> This regex works as expected on "apple!", but returns nil on "apple":
> irb(main):010:0> /^[:alnum:][A-Za-z0-9\-]*[:alnum:]$/ =~ "apple!"
> => nil
> irb(main):011:0> /^[:alnum:][A-Za-z0-9\-]*[:alnum:]$/ =~ "apple"
> => nil
>
> Can anyone explain this?

Yes, that's a classical mistake: POSIX specifications of this kind
must be included inside other []. Try rather

/^[[:alnum:]][A-Za-z0-9\-]*[[:alnum:]]$/

The way you wrote it just meant ':' or 'a' or 'l' or...

Vince

--
Vincent Fourmond, PhD student
http://vincent.fourmon...

Daniel Lucraft

1/10/2007 2:58:00 PM

0


> /^[[:alnum:]][A-Za-z0-9\-]*[[:alnum:]]$/

Incidentally, the dash doesn't need to be escaped in the character class
if you put it first:

/^[[:alnum:]][-A-Za-z0-9]*[[:alnum:]]$/

best,
Dan

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

Jeff

1/10/2007 7:30:00 PM

0

VIncent and Daniel,

Thanks very much.

-- Jeff