[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Inconsistent regexp behavior

Glen Holcomb

8/14/2008 4:40:00 PM

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

I have a script with two lines:

puts "toads suck".gsub(/(\s)/, "\\#{$1}")

puts " a ".gsub(/(\s), "\\#{$1}")


I get broken output for the first:

toads\suck

\ a
Can anyone explain why this is happening. I'm convinced it shouldn't ever
happen.

--
"Hey brother Christian with your high and mighty errand, Your actions speak
so loud, I can't hear a word you're saying."

-Greg Graffin (Bad Religion)

11 Answers

Robert Klemme

8/14/2008 4:56:00 PM

0

On 14.08.2008 18:40, Glen Holcomb wrote:
> [Note: parts of this message were removed to make it a legal post.]
>
> I have a script with two lines:
>
> puts "toads suck".gsub(/(\s)/, "\\#{$1}")
>
> puts " a ".gsub(/(\s), "\\#{$1}")
>
>
> I get broken output for the first:
>
> toads\suck
>
> \ a>
> Can anyone explain why this is happening. I'm convinced it shouldn't ever
> happen.

I'm sorry, but you're wrong. :-) When you pass "\\#{$1}" to gsub the
string is evaluated *once* and even worse, the value of $1 at time of
call has nothing to do with this gsub operation. gsub did not even
start matching at this point in time.

The second replacement apparently works because it gets $1 from the
first execution...

You also do not need the grouping because the group covers the whole
match anyway. You rather want a special replacement expression:

irb(main):001:0> "toads suck".gsub(/\s/, '\\\\\\&')
=> "toads\\ suck"
irb(main):002:0> puts "toads suck".gsub(/\s/, '\\\\\\&')
toads\ suck
=> nil

Note, the high number of backslashes is necessary because there are two
levels of escaping: first the string, i.e. you need two backslashes to
have one backslash in the string. Then you need to escape the backslash
you want to insert into the replacement because backslash is a
metacharacter in the substitution string itself.

Kind regards

robert

Sebastian Hungerecker

8/14/2008 5:01:00 PM

0

Glen Holcomb wrote:
> puts "toads suck".gsub(/(\s)/, "\\#{$1}")

Think about in what order the elements of this expression get evaluated. Then
think about what the value of $1 is when "\\#{$1}" gets evaluated.

HTH,
Sebastian
--
NP: Disbelief - Coast To Coast (Bonus Track)
Jabber: sepp2k@jabber.org
ICQ: 205544826

Glen Holcomb

8/14/2008 5:01:00 PM

0

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

On Thu, Aug 14, 2008 at 10:40 AM, Glen Holcomb <damnbigman@gmail.com> wrote:

> I have a script with two lines:
>
> puts "toads suck".gsub(/(\s)/, "\\#{$1}")
>
> puts " a ".gsub(/(\s), "\\#{$1}")
>
>
> I get broken output for the first:
>
> toads\suck
>
> \ a>
> Can anyone explain why this is happening. I'm convinced it shouldn't ever
> happen.
>
> --
> "Hey brother Christian with your high and mighty errand, Your actions speak
> so loud, I can't hear a word you're saying."
>
> -Greg Graffin (Bad Religion)
>

Oops. Typo, I should probably copy and paste next time. The second gsub is
actually:

puts " a ".gsub(/(\s)/, "\\#{$1}")

--
"Hey brother Christian with your high and mighty errand, Your actions speak
so loud, I can't hear a word you're saying."

-Greg Graffin (Bad Religion)

Glen Holcomb

8/14/2008 6:26:00 PM

0

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

On Thu, Aug 14, 2008 at 11:01 AM, Glen Holcomb <damnbigman@gmail.com> wrote:

> On Thu, Aug 14, 2008 at 10:40 AM, Glen Holcomb <damnbigman@gmail.com>
> wrote:
>
> > I have a script with two lines:
> >
> > puts "toads suck".gsub(/(\s)/, "\\#{$1}")
> >
> > puts " a ".gsub(/(\s), "\\#{$1}")
> >
> >
> > I get broken output for the first:
> >
> > toads\suck
> >
> > \ a> >
> > Can anyone explain why this is happening. I'm convinced it shouldn't
> ever
> > happen.
> >
> > --
> > "Hey brother Christian with your high and mighty errand, Your actions
> speak
> > so loud, I can't hear a word you're saying."
> >
> > -Greg Graffin (Bad Religion)
> >
>
> Oops. Typo, I should probably copy and paste next time. The second gsub
> is
> actually:
>
> puts " a ".gsub(/(\s)/, "\\#{$1}")
>
> --
> "Hey brother Christian with your high and mighty errand, Your actions speak
> so loud, I can't hear a word you're saying."
>
> -Greg Graffin (Bad Religion)
>

Thanks guys, now I know why.

--
"Hey brother Christian with your high and mighty errand, Your actions speak
so loud, I can't hear a word you're saying."

-Greg Graffin (Bad Religion)

Peña, Botp

8/15/2008 12:45:00 AM

0

From: Glen Holcomb [mailto:damnbigman@gmail.com]=20
# puts "toads suck".gsub(/(\s)/, "\\#{$1}")
# toads\suck
# Can anyone explain why this is happening. I'm convinced it=20
# shouldn't ever happen.

it's a common surprise, but ruby has a solution for that; try the block =
form

irb(main):002:0> puts "toads suck".gsub(/(\s)/) { "\\#{$1}" }
toads\ suck
=3D> nil

no surprise.

kind regards -botp

Robert Klemme

8/15/2008 9:18:00 AM

0

2008/8/15 Pe=F1a, Botp <botp@delmonte-phil.com>:
> From: Glen Holcomb [mailto:damnbigman@gmail.com]
> # puts "toads suck".gsub(/(\s)/, "\\#{$1}")
> # toads\suck
> # Can anyone explain why this is happening. I'm convinced it
> # shouldn't ever happen.
>
> it's a common surprise, but ruby has a solution for that; try the block f=
orm
>
> irb(main):002:0> puts "toads suck".gsub(/(\s)/) { "\\#{$1}" }
> toads\ suck
> =3D> nil

I was thinking about including a statement about the block form but
decided against - now you made me write it anyway... :-)

IMHO the block form should be restricted to cases where substitutions
should be done that cannot be implemented with simple pattern
replacement (e.g. inserting a match counter, doing lookups somewhere
etc.). This is mainly because I find the non block form more
appropriate if you are only doing pattern replacements but it also has
the nice side effect of being more efficient. Use the right tool for
the job.

Kind regards

robert

--=20
use.inject do |as, often| as.you_can - without end

MattB

8/3/2012 7:01:00 PM

0

On Fri, 3 Aug 2012 16:31:23 +0000 (UTC), 3109 Dead <dead@gone.com>
wrote:

>On Fri, 03 Aug 2012 07:24:47 -0700, Yak wrote:
>
>> On Aug 2, 6:22?pm, MattB <trdell1234NOMORES...@gmail.com> wrote:
>>> Dems reject GOP move to force layoff notices
>>>
>>> http://seattletimes.nwsource...
>politics/2018832669_apusdefense...
>>>
>>> Senate Democrats rejected a Republican effort to force defense
>>> contractors to send out notices of possible job layoffs four days
>>> before the election, calling the move politically driven and purely
>>> speculative based on looming spending cuts.
>>>
>>> The Senate Appropriations Committee voted 17-13 against an amendment by
>>> Sen. Lindsey Graham, R-S.C. The provision would have overturned Labor
>>> Department guidance this week to federal contractors that they do not
>>> have to warn their employees about potential layoffs from the
>>> automatic, across-the-board cuts that kick in Jan. 2.
>>>
>>> A 1980s law, known as the WARN Act, says those notices would have to go
>>> out 60 days in advance of the cuts, which would put them in workers'
>>> mailboxes four days before the Nov. 6 election.
>>
>> So, once again, obama wants to disregard the law to benefit his re-
>> election bid.
>
>Who, exactly, would he send the notices to? Have they determined who
>will be laid off yet?


You think waiting a week will change who gets the notices. I'm sure
the companies are working on it just in case. This like everything
OBOBO does is about the election. His whole first term has been about
getting a second term, that is why we are in such bad shape. OBOBO
has a agenda nothing else matters.Then he can get "FLEXIBLE" with
Russia and China.

Yoorghis

8/3/2012 8:17:00 PM

0

On Fri, 03 Aug 2012 11:56:42 -0700, MattB
<trdell1234NOMORESPAM@gmail.com> wrote:

>
>Obama doesn't have to obey the law. He's a liberal.

How about Raygun lying about selling arms to Iran?

Was he "obeying law" when he lied (under oath)?

Or Bush when he said: "Iraq has Tons of WMD"---and ignored evidence to
the contrary.

You keep demanding things that you will not, or never have, required
of your own idiots.

IDIOT.

MattB

8/3/2012 8:49:00 PM

0

On Fri, 03 Aug 2012 14:16:42 -0600, Yoorghis@Jurgis.net wrote:

>On Fri, 03 Aug 2012 11:56:42 -0700, MattB
><trdell1234NOMORESPAM@gmail.com> wrote:
>
>>
>>Obama doesn't have to obey the law. He's a liberal.
>
>How about Raygun lying about selling arms to Iran?
>
>Was he "obeying law" when he lied (under oath)?
>
>Or Bush when he said: "Iraq has Tons of WMD"---and ignored evidence to
>the contrary.
>
>You keep demanding things that you will not, or never have, required
>of your own idiots.

So you are excusing Obama with the past actions of others. Well at
least you admit he is a lawbreaker. Well liberals can no longer point
to the past actions of the past without being total Hypocrites.

.




>
>IDIOT.

Yak

8/3/2012 11:50:00 PM

0

On Aug 3, 4:48 pm, MattB <trdell1234NOMORES...@gmail.com> wrote:
> On Fri, 03 Aug 2012 14:16:42 -0600, Yoorg...@Jurgis.net wrote:
> >On Fri, 03 Aug 2012 11:56:42 -0700, MattB
> ><trdell1234NOMORES...@gmail.com> wrote:
>
> >>Obama doesn't have to obey the law.  He's a liberal.
>
> >How about Raygun lying about selling arms to Iran?
>
> >Was he "obeying law" when he lied (under oath)?
>
> >Or Bush when he said: "Iraq has Tons of WMD"---and ignored evidence to
> >the contrary.
>
> >You keep demanding things that you will not, or never have, required
> >of your own idiots.
>
> So you are excusing Obama with the past actions of others.  Well at
> least you admit he is a lawbreaker.  Well liberals can no longer point
> to the past actions of the past without being total Hypocrites.

100% accurate right down the middle of the fairway!

>
> .
>
>
>
>
>
>
>
>
>
> >IDIOT.