The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages - More
> Regex Programming
|
Other - Replacing first three words
Discuss Replacing first three words in the Regex Programming forum on Dev Shed. Replacing first three words Regular expressions forum covering PCRE and POSIX techniques, practices, and standards. Regular expressions help shorten coding time by providing the ability to compact many lines of code into one string.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 18th, 2012, 11:19 AM
|
|
Registered User
|
|
Join Date: Nov 2011
Posts: 22
Time spent in forums: 6 h 14 m 41 sec
Reputation Power: 0
|
|
|
Other - Replacing first three words
Hi I want to replace first three "Next" words to "Previous" as I shown below, I tried some tools but I couldn't what regexes should I use to do this?
Code:
<li style="margin-right: 20px;">
<a href="4.html" title="Next">
<img src="../thumbs/4.JPG" alt="Next" />
<h2>Next</h2>
<div class="clear"></div>
</a>
</li>
<li style="margin-left: 20px;">
<a href="6.html" title="Next">
<img src="../thumbs/6.JPG" alt="Next" />
<h2>Next</h2>
to this:
Code:
<li style="margin-right: 20px;">
<a href="4.html" title="Previous">
<img src="../thumbs/4.JPG" alt="Previous" />
<h2>Previous</h2>
<div class="clear"></div>
</a>
</li>
<li style="margin-left: 20px;">
<a href="6.html" title="Next">
<img src="../thumbs/6.JPG" alt="Next" />
<h2>Next</h2>
|

February 18th, 2012, 01:44 PM
|
 |
Turn left at the third duck
|
|
Join Date: Dec 2011
Location: Nelson, NZ
|
|
Hi again Seamoon!
Please don't cross-post
I replied on the other post as the context made it clear that you are using php.
For reference for this board:
Search:
Code:
(?s)^(?>.*?\b\K(Next))\b(?>(.*?)\b(Next))\b(.*?)\bNext\b
Replace:
Code:
Previous\2Previous\4Previous
Wishing you a fun weekend.
|

February 18th, 2012, 02:43 PM
|
|
Registered User
|
|
Join Date: Nov 2011
Posts: 22
Time spent in forums: 6 h 14 m 41 sec
Reputation Power: 0
|
|
|
Hi ragax, I get this error message:
Regular Expression Syntax Error
Unknown regex token \K. Use backslashes to escape metacharacters
P.S.
Ok, no cross-posting.
|

February 18th, 2012, 03:11 PM
|
 |
Turn left at the third duck
|
|
Join Date: Dec 2011
Location: Nelson, NZ
|
|
Code:
Unknown regex token \K.
\K only works since php 5.2.4, maybe you have an older version?
Here is a version without \K. Note that both the Search and the Replace have changed.
Search:
Code:
(?s)^(?>(.*?)\b(Next))\b(?>(.*?)\b(Next))\b(.*?)\bNext\b
Replace:
Code:
\1Previous\3Previous\5Previous
Let me know if you run into more problems. 
|

February 18th, 2012, 03:22 PM
|
|
Registered User
|
|
Join Date: Nov 2011
Posts: 22
Time spent in forums: 6 h 14 m 41 sec
Reputation Power: 0
|
|
I'm using powergrep for replace operation. This time all nexts replaced by previous.
Code:
<img src="../thumbs/5.JPG" alt="Previous" />
<h2>Previous</h2>
<div class="clear"></div>
</a>
</li>
<li style="margin-left: 20px;">
<a href="7.html" title="Previous">
<img src="../thumbs/7.JPG" alt="Previous" />
<h2>Previous</h2>
<div class="clear"></div>
|

February 18th, 2012, 04:06 PM
|
 |
Turn left at the third duck
|
|
Join Date: Dec 2011
Location: Nelson, NZ
|
|
Not a PG expert, when I tried it I found the interface impossible to understand. (I have a much easier time with ABA Replace.) But that's my own limitation, I probably just need to study the manual. Anyhow...
In the PHP example on the other post, I gave preg_replace a 1 for the fourth parameter, meaning make one replacement. The same is probably possible in PG.
In the meantime, this is heavier but it should work.
Search:
Code:
(?s)\A(?>((?:(?!Next).)*)\b(Next))\b(?>((?:(?!Next).)*)\b(Next))\b((?:(?!Next).)*)\bNext\b
Replace:
Code:
\1Previous\3Previous\5Previous
Let me know if this solves it.

Last edited by ragax : February 18th, 2012 at 04:57 PM.
Reason: performance tweak
|

February 18th, 2012, 04:29 PM
|
|
Registered User
|
|
Join Date: Nov 2011
Posts: 22
Time spent in forums: 6 h 14 m 41 sec
Reputation Power: 0
|
|
Yep, it worked great and saved me editing several hundreds of pages. Thanks thousands for your help and patience ragax! 
|

February 18th, 2012, 04:43 PM
|
 |
Turn left at the third duck
|
|
Join Date: Dec 2011
Location: Nelson, NZ
|
|
|
It's a pleasure, seamoon, glad it worked.
Please don't hesitate to ask if other questions arise.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|