[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Explain this ruby regex

renton.dan

10/3/2008 3:39:00 PM

Can someone explain this regex ...

"one two".scan(/\w*/).length

returns 4. I can see it matching the 2 words and the space, what else
is it matching on? Is there a null terminator, I thought Ruby strings
were not null termed.
8 Answers

Ben Bleything

10/3/2008 3:45:00 PM

0

On Sat, Oct 04, 2008, renton.dan@gmail.com wrote:
> "one two".scan(/\w*/).length
>
> returns 4. I can see it matching the 2 words and the space, what else
> is it matching on? Is there a null terminator, I thought Ruby strings
> were not null termed.

Try replacing #length with #inspect and seeing what the output of scan
is. You'll find that it's returning two empty strings as well. I
suspect what you really want is \w+...

Ben

renton.dan

10/3/2008 4:07:00 PM

0

On Oct 3, 11:44 am, Ben Bleything <b...@bleything.net> wrote:
> On Sat, Oct 04, 2008, renton....@gmail.com wrote:
> > "one two".scan(/\w*/).length
>
> > returns 4. I can see it matching the 2 words and the space, what else
> > is it matching on? Is there a null terminator, I thought Ruby strings
> > were not null termed.
>
> Try replacing #length with #inspect and seeing what the output of scan
> is.  You'll find that it's returning two empty strings as well.  I
> suspect what you really want is \w+...
>
> Ben

Yeah, you're right \w+ will pull out the words, which is what I want
anyway. Though I'm trying to understand what \w* is doing.
irb(main):015:0> "one two".scan(/\w*/).inspect
=> "[\"one\", \"\", \"two\", \"\"]"

My question is, what is the last "\", where does it come from.

Patrick He

10/3/2008 4:29:00 PM

0

\w* does not match the space between string "one" and "two". it matches
"one", <empty string after "one">, "two", <empty string after "two">.

There are some other examples:

irb(main):004:0> "one".scan(/^\w*/)
=> ["one"]
irb(main):005:0> "one".scan(/\w*$/)
=> ["one", ""]


--
Patrick


renton.dan@gmail.com wrote:
> On Oct 3, 11:44 am, Ben Bleything <b...@bleything.net> wrote:
>
>> On Sat, Oct 04, 2008, renton....@gmail.com wrote:
>>
>>> "one two".scan(/\w*/).length
>>>
>>> returns 4. I can see it matching the 2 words and the space, what else
>>> is it matching on? Is there a null terminator, I thought Ruby strings
>>> were not null termed.
>>>
>> Try replacing #length with #inspect and seeing what the output of scan
>> is. You'll find that it's returning two empty strings as well. I
>> suspect what you really want is \w+...
>>
>> Ben
>>
>
> Yeah, you're right \w+ will pull out the words, which is what I want
> anyway. Though I'm trying to understand what \w* is doing.
> irb(main):015:0> "one two".scan(/\w*/).inspect
> => "[\"one\", \"\", \"two\", \"\"]"
>
> My question is, what is the last "\", where does it come from.
>

Patrick Doyle

10/3/2008 4:44:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

The key idea here is that "*" means "match zero or more of" whereas "+"
means "match one or more of". So, when you match \w* against "one two",
there are zero or more instances of a word character (3, in fact, 'o', 'n',
and 'e'), so that produces one result. Following that result, there are
zero matches of a word character, but since you asked for "zero or more of",
you get that empty string result. Later, rinse, repeat for the "two" part.

FWIW, instead of looking at the result with #inspect, I found it more
informative to look at the result returned from #scan by itself, e.g.

irb> "one two".scan(/\w*/)
=> ["one", "", "two", ""]

--wpd


On Fri, Oct 3, 2008 at 12:29 PM, Patrick He <patrick.he@gmail.com> wrote:

> \w* does not match the space between string "one" and "two". it matches
> "one", <empty string after "one">, "two", <empty string after "two">.
>
> There are some other examples:
>
> irb(main):004:0> "one".scan(/^\w*/)
> => ["one"]
> irb(main):005:0> "one".scan(/\w*$/)
> => ["one", ""]
>
>
> --
> Patrick
>
>
> renton.dan@gmail.com wrote:
> > On Oct 3, 11:44 am, Ben Bleything <b...@bleything.net> wrote:
> >
> >> On Sat, Oct 04, 2008, renton....@gmail.com wrote:
> >>
> >>> "one two".scan(/\w*/).length
> >>>
> >>> returns 4. I can see it matching the 2 words and the space, what else
> >>> is it matching on? Is there a null terminator, I thought Ruby strings
> >>> were not null termed.
> >>>
> >> Try replacing #length with #inspect and seeing what the output of scan
> >> is. You'll find that it's returning two empty strings as well. I
> >> suspect what you really want is \w+...
> >>
> >> Ben
> >>
> >
> > Yeah, you're right \w+ will pull out the words, which is what I want
> > anyway. Though I'm trying to understand what \w* is doing.
> > irb(main):015:0> "one two".scan(/\w*/).inspect
> > => "[\"one\", \"\", \"two\", \"\"]"
> >
> > My question is, what is the last "\", where does it come from.
> >
>
>

Brian Candler

10/3/2008 7:46:00 PM

0

> FWIW, instead of looking at the result with #inspect, I found it more
> informative to look at the result returned from #scan by itself, e.g.
>
> irb> "one two".scan(/\w*/)
> => ["one", "", "two", ""]

irb displays the expression value using "inspect", so you are using
inspect even though you didn't ask for it :-)
--
Posted via http://www.ruby-....

Robert Klemme

10/5/2008 4:19:00 PM

0

On 03.10.2008 18:44, Patrick Doyle wrote:
> The key idea here is that "*" means "match zero or more of" whereas "+"
> means "match one or more of". So, when you match \w* against "one two",
> there are zero or more instances of a word character (3, in fact, 'o', 'n',
> and 'e'), so that produces one result. Following that result, there are
> zero matches of a word character, but since you asked for "zero or more of",
> you get that empty string result. Later, rinse, repeat for the "two" part.

It boils down to this statement: a subexpression with "*" potentially
matches an _empty string anywhere_ in a string.

Kind regards

robert

regn.pickford

3/23/2011 6:27:00 PM

0

Eunometic wrote:
> On Mar 22, 10:55 am, jg <j...@nospam.com> wrote:
>> regn.pickford wrote:
>>> From the multicultural heartland in Australia (Melbourne)
>>
>>> http://www.heraldsun.com.au/news/immigration-support-plumme......
>>
>>> [quote st]
>>> With the nation marking Harmony Day today, the Monash University
>>> study has revealed that 52 per cent of
>>> Victorians believe the migrant intake is too high
>>> [quote fin]
>>
>>> A quick read through the comments indicates that figure is seriously
>>> understated.
>>
>>> In NSW, Keneally has said she will accept more immigrants into NSW
>>> and O'Farrell is on record as wanting a BIGGER Australia than
>>> Abbott's BIG Australia.
>>
>>> This is how the Liberals, Nationals, Labour and the Greens plan to
>>> represent you /us.
>>
>>> Forcing you to share your wealth and resources with migrants,.
>>> against your will,
>>> without your mandate or any common sense for that matter.
>>
>>> Immigrants compete with you for your job and undermine your working
>>> conditions.
>>> They send up the price of housing,food,electricity etc and force
>>> couples to live in third world
>>> conditions to compete with them, they make welfare and health
>>> queues longer.
>>
>>> Unsurprisingly, the greatest concern is about Islam and the least
>>> concern is about Poms and Greeks.
>>> who were subject to a sensible and proven effective, compatability
>>> test that was called mistakenly,
>>> the White Australia Policy (Greeks aren't white, neither are a
>>> number of Poms.
>>
>>> So for those in NSW. Immigration IS a State issue and consider how
>>> you want this
>>> State to be like when voting this Saturday (28) and remember;
>>> voting is compulsorary
>>> but inviting millions of incompatable migrants to come and share an
>>> equal portion
>>> of the wealth of Australia , is NOT ...
>>
>> A majority probably don't want a bigger Australia, and yet they will
>> vote for one of two parties which both do.-
>
> Because they both conspire under an agreement called bipartisanship to
> agree on high immigration. And the two parties Labour and Liberal
> that want a bigger Australia both accept big donations from those that
> insist upon it, both cultivate a sense of slight paranoia in ethnic
> groups inorder to draw in the ethnic vote, both gain political capital
> by using strawman racists as punching bags to show their supposed
> moral superiority, both are quick to point the finger, both care about
> their short term political survival and carears not the minoritisation
> of European Australians, High housing prices, over population etc.
>
> Most of all both parties use underhand tactics to misuse the power of
> the state against anti-immigration parties.
>
> It's just about illegal to make an anti immigraion statement.
>

We need to remove minority protection legislation. Having to `mix it`
and fit in with Australians without pulling out the minority `rights' card
didn't do the Greeks, Italians and post ww2 migrants any harm.

> The name of the game in politics is donations to pay for media
> campaigns.
>
My local Labour candidate has sent me five personalised letters,
explaining this and that.

Doesn't know me very well if the goose thinks wasting _taxpayers_
money on glossy leaflets will `win me over`
I do wonder if the printers are good mates of his?




> The Laborals are bad for Australia beyond repair. We do need to
> damage both these parties, maybe get rid of them. God help us if they
> last another 10 years.
>
> NSW labour needs a crushing blow. They deserve it. Its what they do
> to other parties.


z.yxwv

4/1/2011 9:24:00 PM

0

On Mar 24, 4:27 am, "regn.pickford" <r...@mysoul.cop.au> wrote:
> Eunometic wrote:
> > On Mar 22, 10:55 am, jg <j...@nospam.com> wrote:
> >> regn.pickford wrote:
> >>> From the multicultural heartland in Australia (Melbourne)
>
> >>>http://www.heraldsun.com.au/news/immigration-support-plumme.......
>
> >>> [quote st]
> >>> With the nation marking Harmony Day today, the Monash University
> >>> study has revealed that 52 per cent of
> >>> Victorians believe the migrant intake is too high
> >>> [quote fin]
>
> >>> A quick read through the comments indicates that figure is seriously
> >>> understated.
>
> >>> In NSW, Keneally has said she will accept more immigrants into NSW
> >>> and O'Farrell is on record as wanting a BIGGER Australia than
> >>> Abbott's BIG Australia.
>
> >>> This is how the Liberals, Nationals, Labour and the Greens plan to
> >>> represent you /us.
>
> >>> Forcing you to share your wealth and resources with migrants,.
> >>> against your will,
> >>> without your mandate or any common sense for that matter.
>
> >>> Immigrants compete with you for your job and undermine your working
> >>> conditions.
> >>> They send up the price of housing,food,electricity etc and force
> >>> couples to live in third world
> >>> conditions to compete with them, they make welfare and health
> >>> queues longer.
>
> >>> Unsurprisingly, the greatest concern is about Islam and the least
> >>> concern is about Poms and Greeks.
> >>> who were subject to a sensible and proven effective, compatability
> >>> test that was called mistakenly,
> >>> the White Australia Policy (Greeks aren't white, neither are a
> >>> number of Poms.
>
> >>> So for those in NSW. Immigration IS a State issue and consider how
> >>> you want this
> >>> State to be like when voting this Saturday (28) and remember;
> >>> voting is compulsorary
> >>> but inviting millions of incompatable migrants to come and share an
> >>> equal portion
> >>> of the wealth of Australia , is NOT ...
>
> >> A majority probably don't want a bigger Australia, and yet they will
> >> vote for one of two parties which both do.-
>
> > Because they both conspire under an agreement called bipartisanship to
> > agree on high immigration.  And the two parties Labour and Liberal
> > that want a bigger Australia both accept big donations from those that
> > insist upon it, both cultivate a sense of slight paranoia in ethnic
> > groups inorder to draw in the ethnic vote, both gain political capital
> > by using strawman racists as punching bags to show their supposed
> > moral superiority, both are quick to point the finger, both care about
> > their short term political survival and carears not the minoritisation
> > of European Australians, High housing prices, over population etc.
>
> > Most of all both parties use underhand tactics to misuse the power of
> > the state against anti-immigration parties.
>
> > It's just about illegal to make an anti immigraion statement.
>
> We need to remove minority protection legislation.  Having to `mix it`
> and fit in with Australians without pulling out the minority `rights' card
> didn't do the Greeks, Italians and post ww2 migrants any harm.
>
> > The name of the game in politics is donations to pay for media
> > campaigns.
>
> My local Labour candidate has sent me five  personalised letters,
> explaining this and that.
>
> Doesn't know me very well if the goose thinks wasting _taxpayers_
>  money on glossy  leaflets will `win me over`
> I do wonder if the printers are good mates of his?
>
>
>
> > The Laborals are bad for Australia beyond repair.  We do need to
> > damage both these parties, maybe get rid of them.  God help us if they
> > last another 10 years.
>
> > NSW labour needs a crushing blow.  They deserve it.  Its what they do
> > to other parties.- Hide quoted text -
>
> - Show quoted text -- Hide quoted text -
>
> - Show quoted text -

s119?

http://www.theaustralian.com.au/national-affairs/how-i-lost-faith-in-multiculturalism/story-fn59niix-122...