You are not logged in.

#1 2009-03-06 18:32:50

Reploid
Member
From: Cold Country up North
Registered: 2008-03-27
Posts: 110

n00b Vi Q/reg expression for html

How do I wrap <p> </p> tags around paragraphs globally??

And when I try to wrap <i></i> around a line, i get the error E488: trailing characters.

:28,32s/.*/<i>&</i>/g

. I thought this would wrap italic markers around the whole lines from 28 to 32, what am I doing wrong?

Oh, and if anyone has a link to a good tutorial for n00bs on global substitutions by regular expressions, don't keep it to yourselves. :-)

Last edited by Reploid (2009-03-06 18:39:35)

Offline

#2 2009-03-06 18:40:25

Mr.Elendig
#archlinux@freenode channel op
From: The intertubes
Registered: 2004-11-07
Posts: 4,092

Re: n00b Vi Q/reg expression for html

Reploid wrote:

How do I wrap <p> </p> tags around paragraphs globally??

And when I try to wrap <i></i> around a line, i get the error E488: trailing characters.

:28,32s/.*/<i>&</i>/g

. I thought this would wrap italic markers around the whole lines from 28 to 32, what am I doing wrong?

Oh, and if anyone has a link to a good tutorial for n00bs on global substitutions by regular expressions, don't keep it to yourselves. :-)

You have to escape the / in </i>, or use another delimiter.


Evil #archlinux@libera.chat channel op and general support dude.
. files on github, Screenshots, Random pics and the rest

Offline

#3 2009-03-06 18:51:25

Reploid
Member
From: Cold Country up North
Registered: 2008-03-27
Posts: 110

Re: n00b Vi Q/reg expression for html

28,37s/.*/<i>&<\/i>/g

It worked!! big_smile This will same me of TONS of time, yay! Thanks!!!!

What would I put in there to signal that I want to replace a paragraph?

EDIT: cheated a little:

:%s /^$/<\/p><p>

inserting the paragraph markers on the blank lines instead of around paragraphs, and just inserting the first <p> manually.

Last edited by Reploid (2009-03-06 22:19:56)

Offline

#4 2009-03-06 22:27:03

Husio
Member
From: Europe
Registered: 2005-12-04
Posts: 359
Website

Re: n00b Vi Q/reg expression for html

Offline

#5 2009-03-08 12:53:19

Reploid
Member
From: Cold Country up North
Registered: 2008-03-27
Posts: 110

Re: n00b Vi Q/reg expression for html

Thx @ Husio. Will experiment with script a little later, trying to learn the basic stuff for now.

Another question:

I have sentences like:

 <p><span class=font25>4th completely revised edition</span></p>

However, I can only seem to substitute the sentences with font0-9. (!)

This doesn't work:
:%s<span class=font[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,2
5,26,27,28,29,30]\+><b>\(.*\)<\/b><\/span>/\1/

This doesn't work:
:%s<span class=font[0-9][0-9]*>

This doesn't work:
<span class=font[0-9*][0-9*]>

What would help me get rid of the formatting markup?? yikes

Last edited by Reploid (2009-03-08 12:54:02)

Offline

#6 2009-03-08 13:01:13

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: n00b Vi Q/reg expression for html

Do you want to get rid of all tags?
Try this:

:%s/<[^>]*>//g

Offline

#7 2009-03-08 13:41:37

Reploid
Member
From: Cold Country up North
Registered: 2008-03-27
Posts: 110

Re: n00b Vi Q/reg expression for html

EDIT: I just wanted to remove a few types of tags, but by modifying your script a little, I could make it work by a few keypresses. Thank you so VERY VERY much.

(I did

 :%s/<span[^>]*>//g

and:

:%s/<\/span[^>]*>//g

)

But, seeing as I am trying to learn this stuff, and not just have ppl script for me, how would I go about to include a range from 0-99 in my pattern? [0-9][0-9] didn't work??

Another thing that I spent the entire yesterday, trying to figure out, was how to take the name "super heading" and create "super_heading.html" out of it, making it apply to all the "super duper duper headings" as well.

Last edited by Reploid (2009-03-08 14:15:48)

Offline

#8 2009-03-08 13:47:22

rson451
Member
From: Annapolis, MD USA
Registered: 2007-04-15
Posts: 1,233
Website

Re: n00b Vi Q/reg expression for html

[0-9]+ will match any number, any amount of characters (at least one).


archlinux - please read this and this — twice — then ask questions.
--
http://rsontech.net | http://github.com/rson

Offline

#9 2009-03-08 14:10:54

Reploid
Member
From: Cold Country up North
Registered: 2008-03-27
Posts: 110

Re: n00b Vi Q/reg expression for html

rson451, that doesn't seem to work.

I created a testfile to eliminate the chance of me typing something unintentionally looking just like this:

font
font23
font55
font1
font9
font8


... and then I type
:%s/font[0-9]+/I love reg ex/

It does not replace the font23, font1 etc with "I love reg ex" , it just spits out E486: pattern not found: font[0-9]+

Offline

#10 2009-03-08 14:20:59

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: n00b Vi Q/reg expression for html

in :s it has to be \+
:%s/[0-9]\+/99/g should work
same as :%s/[0-9][0-9]*/99/g

The biggest problem is that regex is greedy, and that doesn't go well with html/xml.

So in tags matching for [^>]* is important.

About issue 2, if you take
<h1>s u m m e r holiday</h1>
And want to convert it to
<a href="s_u_m_m_e_r_holiday">s u m m e r holiday</a>

I don't know actually. I think vim is lacking strength.

You should try to do it with sed.

sed '{s~<h1>\([^<]*\)</h1>~\1~;T;h;s~ ~_~g;s~.*~<a href="&">~;G;s~$~</a>~;s~\n~~}' FILE.html

Offline

#11 2009-03-08 14:56:57

Reploid
Member
From: Cold Country up North
Registered: 2008-03-27
Posts: 110

Re: n00b Vi Q/reg expression for html

The \+ worked, so I must have done something else wrong before, as I tried the \+ on my huge textfile. But the other solution you presented was a lot more elegant, so I'll just stick with that. I read a bit about the greedy stuff in the O'Reilly textbook, so I'll brush up on that to consolidate what I've learned so far. As for the underscore, well, at least I know there is a reason why I couldn't find something applying. tongue

Thx 4 helping me out.

Offline

Board footer

Powered by FluxBB