LINUX.IE, website of the Irish Linux Users' Group
Tux rules!

   
Home
New Users
Articles
Download
Projects
Community
Vendors

  Print Version
Email to...
 
Archives:


planetILUG

Recent News

News Archive


Join the
ILUG
on FaceBook


Join the
ILUG
on LinkedIn


Join the
ILUG SETI
Group



















 
 :: Mailing Lists

[ILUG] Regex Question

[ILUG] Regex Question

John Tobin tobinjt at netsoc.tcd.ie
Tue Sep 28 16:14:08 IST 2004


On Mon, Sep 27, 2004 at 02:39:15PM +0100, Rory Winston wrote:
> Regex gurus,
> 
> I'm trying to do a simple substitute in Vim. I hav a HTML page with hundreds
> of lines like:
> 
> <div class="foo"></div>
> <div class="foo"></div>
> <div class="foo"></div>
> <div class="foo"></div>
> 
> What I would like to be able to do is replace every n'th element with
> something else. So if n = 3, then the result is:
> 
> <div class="foo"></div>
> <div class="foo"></div>
> <div class="foo">TEST</div>
> <div class="foo"></div>
> <div class="foo"></div>
> <div class="foo">TEST</div>
> 
> Does anybody know how I might achieve this?

You'll need to use sub-replace-special and call a function from it.  Put
this in a file somewhere:

function! My_replace()
        if line ( "." ) % 3 == 0
                return ">TEXT<"
        else
                return "><"
        endif
endfunction

Source it with :source ~/some/file
Then substitute like so:
:%s/></\=My_replace()/

For more information see:
:help sub-replace-special
:help :function
sub-replace-special could use some more documentation.

-- 
John Tobin
"No no no. You're supposed to test with -march=... -fomit-frame-pointer
-ffancy-math -fuse-lots-of-resources-go-very-fast -fsacrifice-more-goats
-fsummon-cthulu-if-that-helps as root at nice -20, preferably in single
user mode and jumps should be aligned on pentagrams, not 8 byte
boundaries.
Definitely not use debugging :-)"
				      Nicholas Clark, in perl6-internals



More information about the ILUG mailing list
Read this without the formatting.
                                                                                                    

 

Hosted by HEAnet


Maintained by the ILUG website team. The aim of Linux.ie is to support and help commercial and private users of Linux in Ireland. You can display ILUG news in your own webpages, read backend information to find out how. Networking services kindly provided by HEAnet, server kindly donated by Dell. Linux is a trademark of Linus Torvalds, used with permission. No penguins were harmed in the production or maintenance of this highly praised website. Looking for the Indian Linux Users' Group? Try here. If you've read all this and aren't a lawyer: you should be!
RSS Version
Powered by Dell