non-greedy regular expression help needed

0 views
Skip to first unread message

kurt krueckeberg

unread,
Nov 7, 2009, 8:43:44 PM11/7/09
to vim_use
I want rmove the "<FONT SIZE=3"> from the line below

<FONT SIZE=3>some text goes here</FONT>

so it looks like:

some text goes here</FONT>

Actually, I prefer to also remove "</FONT>", so I just have:
some text goes here

How do I compose a non-greedy regular expression to find "<FONT
SIZE=3>"? I've tried

:%s/<FONT\(.*\)\{-1}>//g

But this eliminates the entire line.

thanks,
Kurt

Tim Chase

unread,
Nov 7, 2009, 11:05:01 PM11/7/09
to vim...@googlegroups.com
kurt krueckeberg wrote:
> I want rmove the "<FONT SIZE=3"> from the line below
>
> <FONT SIZE=3>some text goes here</FONT>
>
> so it looks like:
>
> some text goes here</FONT>
>
> Actually, I prefer to also remove "</FONT>", so I just have:
> some text goes here
>
> How do I compose a non-greedy regular expression to find "<FONT
> SIZE=3>"? I've tried
>
> :%s/<FONT\(.*\)\{-1}>//g
^
This "*" is greedy. I think you want

:%s@</\=FONT.\{-}>@@g

which is

< a literal "<"
/\= an optional "/"
FONT the literal tag
.\{-} any character, as few as possible
> a literal ">"

You might put optional whitespace before the slash or FONT, making it

:%s@<\s*/\=\s*FONT.\{-}>@@g

and optionally allow case-insensitivity with the "\c" token:

:%s@\c<\s*/\=\s*FONT.\{-}>@@g

As a non-greedy alternative, you might also consider something
like the character-class "[^>]"

:%s@<\s*/\=\s*FONT[^>]*>@@g

Hope this gives you what you need as well as some ways to riff on
the theme.

-tim

John Beckett

unread,
Nov 8, 2009, 1:19:06 AM11/8/09
to vim...@googlegroups.com
kurt krueckeberg wrote:
> I want rmove the "<FONT SIZE=3"> from the line below
>
> <FONT SIZE=3>some text goes here</FONT>
>
> so it looks like:
>
> some text goes here</FONT>
>
> Actually, I prefer to also remove "</FONT>", so I just have:
> some text goes here

Adding to what Tim said, the following should just leave the
text between the FONT tags:

:%s@</\=FONT.\{-}>\(.\{-}\)</FONT>@\1@g

John

Francisco Dibar

unread,
Nov 8, 2009, 8:45:33 AM11/8/09
to vim...@googlegroups.com
On Sat, Nov 7, 2009 at 10:43 PM, kurt krueckeberg <ku...@pobox.com> wrote:
>
> I want rmove the "<FONT SIZE=3"> from the line below
>
>  <FONT SIZE=3>some text goes here</FONT>
>
> so it looks like:
>
>  some text goes here</FONT>
>
> Actually, I prefer to also remove "</FONT>", so I just have:
>  some text goes here
>

Hi Kurt, you can do that using the surround plugin, and entering dst
over the line to Delete the Surrounding html Tag.

kurt krueckeberg

unread,
Nov 8, 2009, 10:24:05 AM11/8/09
to vim_use
> This "*" is greedy.  I think you want
>
>    :%s@</\=FONT.\{-}>@@g
>
> which is
>
>   <     a literal "<"
>   /\=   an optional "/"
>   FONT  the literal tag
>   .\{-} any character, as few as possible
>   >     a literal ">"
>
> You might put optional whitespace before the slash or FONT, making it
>
>    :%s@<\s*/\=\s*FONT.\{-}>@@g
>
> and optionally allow case-insensitivity with the "\c" token:
>
>    :%s@\c<\s*/\=\s*FONT.\{-}>@@g
>
> As a non-greedy alternative, you might also consider something
> like the character-class "[^>]"
>
>    :%s@<\s*/\=\s*FONT[^>]*>@@g
>
> Hope this gives you what you need as well as some ways to riff on
> the theme.
>
> -tim

Tim

Thanks a lot. That worked great. Quick question. Is this correct: @
can be used instead of / a the separater between the searched-for text
and the replacement text, and when so used, this allows one to use /
as a literal?

Kurt

kurt krueckeberg

unread,
Nov 8, 2009, 10:45:19 AM11/8/09
to vim_use
> Hi Kurt, you can do that using the surround plugin, and entering dst
> over the line to Delete the Surrounding html Tag.

Forgive my ignorance, but where would one download the 'dst' plugin?

thanks,
Kurt

Tim Chase

unread,
Nov 8, 2009, 12:05:25 PM11/8/09
to vim...@googlegroups.com
> Thanks a lot. That worked great. Quick question. Is this correct: @
> can be used instead of / a the separater between the searched-for text
> and the replacement text, and when so used, this allows one to use /
> as a literal?

Yes, vim allows a pretty broad range of characters to be used as
the separator as detailed at

:help E146

(not a very intuitive help-target, but it's the most direct link
to the apropos section). I usually fall back to "@", "!" or "#"
depending on what is (or more importantly, what *isn't*) in my
regex/replacement. Shebang lines in shell-scripts eat most of my
popular choices, being of the form "#!/path/to/executable"

-tim

Benoit Mortgat

unread,
Nov 8, 2009, 4:26:09 PM11/8/09
to vim...@googlegroups.com
On Sun, Nov 8, 2009 at 16:45, kurt krueckeberg <ku...@pobox.com> wrote:

Forgive my ignorance, but where would one download the 'dst' plugin?

Hello,
it's called surround.vim and you may find it at vim.org

Have a nice day
--
Benoit Mortgat
39, cours Albert Thomas
69003 Lyon, France
+33 4 27 78 31 27 / +33 6 17 15 41 58
Reply all
Reply to author
Forward
0 new messages