Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Regexp riddle; escaping escapes
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  4 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Phlip  
View profile  
 More options Aug 17 2007, 10:29 am
Newsgroups: comp.lang.ruby
From: Phlip <phlip2...@gmail.com>
Date: Fri, 17 Aug 2007 23:29:58 +0900
Local: Fri, Aug 17 2007 10:29 am
Subject: Regexp riddle; escaping escapes
Rubies:

Someone didn't escape their & in their HTML correctly. Let's fix it.

This regexp correctly does not escape &dude, because we only want to escape
raw & markers:

  p "yo &dude".gsub(/&([^a-z])/i, '&amp;\1')

That passed "yo &dude" thru unchanged. (I am aware "dude" has no ; on the
end; we are leaving that optional, for whatever reason...)

Now escape & followed by a non-alphabetic character:

  p "yo & dude".gsub(/&([^a-z])/i, '&amp;\1')

That correctly provides: "yo &amp; dude"

Now how to escape "yo && dude"? Note that the ([^a-z]) consumes the second
&, leading to this incorrect output:

  "yo &amp;& dude"

The only workaround I can think of is to run the Regexp twice:

  x = "yo && dude"
  2.times{ x.gsub!(/&([^a-z])/i, '&amp;\1') }
  p x

Can someone help my feeb Regexp skills and get a "yo &amp;&amp; dude" in one
line?

--
 Phlip
 http://www.oreilly.com/catalog/9780596510657/
 ^ assert_xpath
 http://tinyurl.com/23tlu5  <-- assert_raise_message


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tim Pease  
View profile  
 More options Aug 17 2007, 10:53 am
From: "Tim Pease" <tim.pe...@gmail.com>
Date: Fri, 17 Aug 2007 23:53:35 +0900
Local: Fri, Aug 17 2007 10:53 am
Subject: Re: Regexp riddle; escaping escapes
On 8/17/07, Phlip <phlip2...@gmail.com> wrote:

str = "yo && dude"
str.gsub!( %r/&(?=[^a-z])/i, '&amp;')
p str
=> "yo &amp;&amp; dude"

The regular expression trick here is the (?=re)  That's called the
"zero-width positive lookahead". It matches, but it does not consume
the string; so the gsub! will only replace the characters that are NOT
inside (?=re).

Blessings,
TwP


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Phlip  
View profile  
 More options Aug 17 2007, 11:00 am
From: Phlip <phlip2...@gmail.com>
Date: Sat, 18 Aug 2007 00:00:47 +0900
Local: Fri, Aug 17 2007 11:00 am
Subject: Re: Regexp riddle; escaping escapes

Tim Pease wrote:
> str.gsub!( %r/&(?=[^a-z])/i, '&amp;')

Thanks!

> "zero-width positive lookahead"

Man, that was right there, but I was blocking on it. (-;

--
 Phlip
 http://www.oreilly.com/catalog/9780596510657/
 ^ assert_xpath
 http://tinyurl.com/yrc77g  <-- assert_latest Model


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tim Pease  
View profile  
 More options Aug 17 2007, 11:38 am
From: "Tim Pease" <tim.pe...@gmail.com>
Date: Sat, 18 Aug 2007 00:38:47 +0900
Local: Fri, Aug 17 2007 11:38 am
Subject: Re: Regexp riddle; escaping escapes
On 8/17/07, Phlip <phlip2...@gmail.com> wrote:

> Tim Pease wrote:

> > str.gsub!( %r/&(?=[^a-z])/i, '&amp;')

> Thanks!

> > "zero-width positive lookahead"

> Man, that was right there, but I was blocking on it. (-;

I had to pull my pickaxe off the shelf and look it up, too.  Page 327
in the second edition if you're interested in reading about it.  It's
in the first edition, too, that is available online.

Blessings,
TwP


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »