On Sun, Apr 19, 2009 at 10:56 PM, John Smith
<rails-mailing-l...@andreas-s.net> wrote:
>
> Andrew Timberlake wrote:
>> On Sat, Apr 18, 2009 at 9:35 AM, John Smith
>> <rails-mailing-l...@andreas-s.net> wrote:
>>>
>>> I'll appreciate any help.
>>
>> Use word break matches in your regular expression as follows:
>>
>> <string>.gsub(/\bcar\b/, 'bike')
>>
>> Andrew Timberlake
>> http://ramblingsonrails.com
>> http://www.linkedin.com/in/andrewtimberlake
>>
>> "I have never let my schooling interfere with my education" - Mark Twain
>
> Thanks. What if I have...?
>
> "Lorem ipum car. Locar ipsum,<p>car lorem ipusum</p>.Lorem car ipsum.
> Lorem <a href='#'>car</a> ipsum."
>
> And now I want to obtain the same but I do not want to obtain <a
> href='#'>bike</a>,
>

This will start to get tricky as it becomes important to know what the
rules really are.
Are you wanting to avoid car surrounded by an anchor tag, or car
surrounded by any tag.
<p>car lorem ipusum</p> will pose a problem as it's half surrounded by a tag.

so to satisfy your test case, the following works by making sure that
car is not followed by a '<' which will work on both cases mentioned
above.

<string>.gsub(/\bcar\b(?=[^<])/, 'bike')
This checks that car is surrounded by a word-break but is not followed by a '<'

If, in Ruby 1.9, I do the following:
<string>.gsub(/(?<!>)\bcar\b(?=[^<])/, 'bike')
I am now checking that car is surrounded by a word-break and is not
preceded by a '>' and not followed by a '<' however this pattern will
not replace "<p>car lorem" with "<p>bike lorem"

Andrew Timberlake
http://ramblingsonrails.com
http://www.linkedin.com/in/andrewtimberlake

"I have never let my schooling interfere with my education" - Mark Twain

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to