[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

What's happening here? A strange regexp problem wth posix :alpha: character class

Rick DeNatale

8/4/2006 7:49:00 PM

I just ran across this, and can't figure out what's happening. the
show_regexp method is as shown on p73 of the Pickaxe 2nd ed.

irb(main):013:0> show_regexp("comparison is", /[:alpha:][^.?!\s'"]*/)
=> "com<<parison>> is"
irb(main):014:0> show_regexp("comparison is", /[:alpha:][^\s]*/)
=> "com<<parison>> is"
irb(main):015:0> show_regexp("comparison is", /[:alpha:][\S]*/)
=> "com<<parison>> is"
irb(main):016:0> show_regexp("comparison is", /[a-zA-Z][\S]*/)
=> "<<comparison>> is"
irb(main):017:0> show_regexp("comparison is", /[a-zA-Z][^\s]*/)
=> "<<comparison>> is"

It looks like the Posix :alpha: character class doesn't include c, o, or m?!?

Or am I missing something?

--
Rick DeNatale

4 Answers

Brian Palmer

8/4/2006 7:59:00 PM

0

Hey Rick,

On Aug 4, 2006, at 1:48 PM, Rick DeNatale wrote:

> I just ran across this, and can't figure out what's happening. the
> show_regexp method is as shown on p73 of the Pickaxe 2nd ed.
>
> irb(main):013:0> show_regexp("comparison is", /[:alpha:][^.?!\s'"]*/)
> => "com<<parison>> is"
> irb(main):014:0> show_regexp("comparison is", /[:alpha:][^\s]*/)
> => "com<<parison>> is"
> irb(main):015:0> show_regexp("comparison is", /[:alpha:][\S]*/)
> => "com<<parison>> is"
> irb(main):016:0> show_regexp("comparison is", /[a-zA-Z][\S]*/)
> => "<<comparison>> is"
> irb(main):017:0> show_regexp("comparison is", /[a-zA-Z][^\s]*/)
> => "<<comparison>> is"
>
> It looks like the Posix :alpha: character class doesn't include c,
> o, or m?!?
>
> Or am I missing something?
>
> --
> Rick DeNatale
>


You need another pair of brackets around the :alpha:

So like this:

show_regexp("comparison is", /[[:alpha:]][\S]*/)

That tripped me up when I first used the Posix classes in Ruby, too.

-- Brian




William James

8/4/2006 8:48:00 PM

0

Brian Palmer wrote:
> Hey Rick,
>
> On Aug 4, 2006, at 1:48 PM, Rick DeNatale wrote:
>
> > I just ran across this, and can't figure out what's happening. the
> > show_regexp method is as shown on p73 of the Pickaxe 2nd ed.
> >
> > irb(main):013:0> show_regexp("comparison is", /[:alpha:][^.?!\s'"]*/)
> > => "com<<parison>> is"
> > irb(main):014:0> show_regexp("comparison is", /[:alpha:][^\s]*/)
> > => "com<<parison>> is"
> > irb(main):015:0> show_regexp("comparison is", /[:alpha:][\S]*/)
> > => "com<<parison>> is"
> > irb(main):016:0> show_regexp("comparison is", /[a-zA-Z][\S]*/)
> > => "<<comparison>> is"
> > irb(main):017:0> show_regexp("comparison is", /[a-zA-Z][^\s]*/)
> > => "<<comparison>> is"
> >
> > It looks like the Posix :alpha: character class doesn't include c,
> > o, or m?!?
> >
> > Or am I missing something?
> >
> > --
> > Rick DeNatale
> >
>
>
> You need another pair of brackets around the :alpha:
>
> So like this:
>
> show_regexp("comparison is", /[[:alpha:]][\S]*/)
>
> That tripped me up when I first used the Posix classes in Ruby, too.

It's not just Ruby that requires the extra brackets.

echo BEGIN{print "123 f 987" ~ /[[:alpha:]]/} | gawk -f -
1

Brian Palmer

8/4/2006 9:49:00 PM

0


On Aug 4, 2006, at 2:50 PM, William James wrote:

>
> It's not just Ruby that requires the extra brackets.
>
> echo BEGIN{print "123 f 987" ~ /[[:alpha:]]/} | gawk -f -
> 1
>

Yeah, I figured it was probably pretty standard, it really makes
sense--otherwise, the regex parser wouldn't be able to tell if the
programmer really just wants to match against the specific
characters :, a, l, p, and h.

-- Brian




Rick DeNatale

8/5/2006 1:12:00 AM

0

Thanks all, that makes sense.

--
Rick