LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-04-2006, 04:48 AM   #1
chr15t0
Member
 
Registered: Jun 2002
Location: London
Distribution: Slackware
Posts: 201

Rep: Reputation: 30
regexp quick fix, identifying elements in html


guys -

I know there are modules on cpan which will help with this, but I just want to know if it can be done with a pure regex.. a guy at worked asked me this yesterday - he just wanted a quick oneliner. I tried and got stuck and now it's really niggling me.. anyway, what do you reckon:

A page of html contains sections like this:

<span>bla foo bla bla foo moo</span><div> bla foo bla bla foo</div>

The challenge is to write a regexp which will replace all occurences of 'foo' with 'bar', but only if between div tags - ie the ones in the span won't be affected.

Does anyone think that's possible in a single expression?


christo
 
Old 06-04-2006, 11:40 AM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
http://www.regular-expressions.info/refadv.html

check the lookaround (lookbehind) section
or
the section on \G

These allow you to specify that your foo occurs after a </splat>
You can also use sed, it has syntax to look for a second foo on a line
Code:
's/foo/bar/2'  replace #2 foo
sed 's/\(.*\)foo/\1bar/'   replace last foo

Last edited by jim mcnamara; 06-04-2006 at 11:44 AM.
 
Old 06-06-2006, 03:35 AM   #3
chr15t0
Member
 
Registered: Jun 2002
Location: London
Distribution: Slackware
Posts: 201

Original Poster
Rep: Reputation: 30
if anybody's wondering, I fixed it with the following expression:

1 while s/&(?=[\w&]+<\/link>)/&amp;/g;

if you want to see it in action, just run the following script:

Code:
#!/usr/bin/perl

$_="<title>foo&bar</title><link>but&thisand&this</link><link>foo&barfsfoo&badrffoo&basrfoo&bar</link><link>booo&thisand&this</link>";

print "$_\n";
1 while s/&(?=[\w&]+<\/link>)/&amp;/g;
print "$_\n";

christo
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
suid java program- a quick fix? lightningdan Linux - General 1 06-01-2006 02:29 PM
LXer: Linux WiFi fix won't be quick LXer Syndicated Linux News 0 05-05-2006 06:33 PM
fix CSS HTML built webpage to render in IE titanium_geek Programming 5 01-30-2006 05:33 PM
Solution to Quick Fix MadnessASAP Linux - Software 1 06-03-2005 08:08 PM
Flashplayer in Fedora - quick fix that worked! TreeDragon60 Fedora 10 06-15-2004 03:24 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:30 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration