#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2011
    Posts
    22
    Rep Power
    0

    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>
  2. #2
  3. Turn left at the third duck
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Location
    Nelson, NZ
    Posts
    112
    Rep Power
    93
    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.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2011
    Posts
    22
    Rep 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.
  6. #4
  7. Turn left at the third duck
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Location
    Nelson, NZ
    Posts
    112
    Rep Power
    93
    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.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2011
    Posts
    22
    Rep 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>
  10. #6
  11. Turn left at the third duck
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Location
    Nelson, NZ
    Posts
    112
    Rep Power
    93
    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
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2011
    Posts
    22
    Rep Power
    0
    Yep, it worked great and saved me editing several hundreds of pages. Thanks thousands for your help and patience ragax!
  14. #8
  15. Turn left at the third duck
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Location
    Nelson, NZ
    Posts
    112
    Rep Power
    93
    It's a pleasure, seamoon, glad it worked.
    Please don't hesitate to ask if other questions arise.

IMN logo majestic logo threadwatch logo seochat tools logo