Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

Coding Forum


You are currently viewing our Coding Forum as a guest. Please register to participate.
Login



Reply
Regular Expression Search & Replace Example | Help Needed
Old 11-25-2008, 12:36 PM Regular Expression Search & Replace Example | Help Needed
Novice Talker

Posts: 11
Name: Chris
Trades: 0
I'm using a wordpress plugin which will search and replace HTML using regular expression. You simply input something in a Search box, and something in a replace box. The type of regular expression is this.

I'm looking to do a relatively simple search and replace using regular expression which will be something like this (in plainish language).

Find content between and including two Search Strings:
SEARCH-STRING-A<-----Any-Content----->SEARCH-STRING-B
Replace All (including Search-String-A and Search-String-B With:
Any Text


Worked Example

Search Strings
Code:
Search-String-A =  <div><a href="http://site.com">
Search-String-B = </img></a></div>
Replace With: <p>I rock!</p>
Example HTML original code:

Code:
<p>Blah blah blah blah</p>
<img src="http://url.com"></img>
<p>Blah blah blah!</p>
<div>
<a href="http://site.com">click here</a> and go to the site. Also see: <img src="http://picture.com">  </img>
</a>    </div>
<p>Blah blah boo boo blah!</p>
After Search and Replace:

Code:
<p>Blah blah blah blah</p>
<img src="http://url.com"></img>
<p>Blah blah blah!</p>
<p>I rock!</p>
<p>Blah blah blah boo boo blah!</p>
The above search and replace must ignore any line breaks or white space in the HTML when looking for the occurence of the 'search string'.

Can anyone give the regular expression code that I will need to put into the Search box, and the replace box to get the above outcome?

Last edited by mrsolutions; 11-25-2008 at 12:38 PM..
mrsolutions is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 11-25-2008, 12:45 PM Re: Regular Expression Search & Replace Example | Help Needed
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,528
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Quote:
The above search and replace must ignores ignore any line breaks or white space in the 'search string'
A regular expression that ignores line breaks AND whitespace will select ALL the text between the first instance of the "startstring" and the LAST instance of the "endstring"

Also "</img>" will never exist in a HTML page.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 11-25-2008, 12:51 PM Re: Regular Expression Search & Replace Example | Help Needed
Novice Talker

Posts: 11
Name: Chris
Trades: 0
Quote:
Originally Posted by chrishirst View Post
A regular expression that ignores line breaks AND whitespace will select ALL the text between the first instance of the "startstring" and the LAST instance of the "endstring"
Thanks for answering. You can see from the example what I'm looking for. The end string might have spaces and line breaks in so those need to be ignored when searching. The search will be set so that the endstring only occurs once. So it is the last and only occurence of that code sequence.

Quote:
Also "</img>" will never exist in a HTML page.
Maybe it's not supposed to but in the XHTML code that's being replaced it does occur. But it doesn't matter anyway - it could be any HTML code.
mrsolutions is offline
Reply With Quote
View Public Profile
 
Old 11-25-2008, 01:03 PM Re: Regular Expression Search & Replace Example | Help Needed
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,528
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
With designing regular expressions, especially ones that are deliberately "greedy", it DOES matter.
Quote:
Maybe it's not supposed to but in the XHTML code that's being replaced it does occur
Shouldn't be there in XHTML either.

Is this actually XML? because there are better methods of reading and replacing nodes than reqular expressions.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 11-25-2008, 01:13 PM Re: Regular Expression Search & Replace Example | Help Needed
Novice Talker

Posts: 11
Name: Chris
Trades: 0
Quote:
Shouldn't be there in XHTML either.
It is in there. I can't do anything about it. I'm just trying to use a regular expression search and replace to get it out.

Quote:
Is this actually XML? because there are better methods of reading and replacing nodes than reqular expressions.
Regular Expression is the ONLY option I have. I am using pre-built browser based software to search and replace HTML content.

It has a search and replace box where I put in something to search for, and put in something to replace it with, then I hit submit. This search and replace can just do plain text, but also regular expression too. So I'm just looking for what I need to put in the boxes to get the desired effect above.
mrsolutions is offline
Reply With Quote
View Public Profile
 
Old 11-26-2008, 01:08 PM Re: Regular Expression Search & Replace Example | Help Needed
Novice Talker

Posts: 11
Name: Chris
Trades: 0
Someone gave me this:

Raw Match Pattern:
<div>\s*<a href="http://site.com">[\S\s]*?</img>\s*</a>\s*</div>

Raw Replace Pattern:
<p>I rock!</p>

$sourcestring after replacement:
<p>Blah blah blah blah</p>
<img src="http://url.com"></img>
<p>Blah blah blah!</p>
<p>I rock!</p>
<p>Blah blah boo boo blah!</p>
mrsolutions is offline
Reply With Quote
View Public Profile
 
Old 11-26-2008, 02:31 PM Re: Regular Expression Search & Replace Example | Help Needed
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,528
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
looks ok, just be aware that everything from the first instance of " <div>\s*<a href="http://site.com"> " to the last instance of " </img>\s*</a>\s*</div> " will be replaced.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 11-27-2008, 11:58 AM Re: Regular Expression Search & Replace Example | Help Needed
Novice Talker

Posts: 11
Name: Chris
Trades: 0
Quote:
Originally Posted by chrishirst View Post
looks ok, just be aware that everything from the first instance of " <div>\s*<a href="http://site.com"> " to the last instance of " </img>\s*</a>\s*</div> " will be replaced.
That should be okay since it noly occurs once and the length of HTML being searches is pretty short.
mrsolutions is offline
Reply With Quote
View Public Profile
 
Old 11-27-2008, 01:20 PM Re: Regular Expression Search & Replace Example | Help Needed
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,528
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Ok then, Try it and let us know
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Reply     « Reply to Regular Expression Search & Replace Example | Help Needed
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.29428 seconds with 12 queries