Saturday, March 2, 2013

Re: Regex or other vim help: limiting the scope of a substitution

On 2013-03-02 13:58, Hsiu Dai wrote:
> <a href="linkone" class="class_a class_b"></a>
> <a href="link_two" class="classc class_d"></a>
>
> And in the end, I'd like to to be:
>
> <a href="linkone" class="class-a class-b"></a>
> <a href="link_two" class="classc class-d"></a>
>
> As you can see, the underscores have been changed to hyphens but
> only in the class attribute, everywhere else they're left alone and
> everything else in the class attribute is also left alone.

Your guesses were pretty close. The magic resides within

:help sub-replace-special

where you learn you can do things like

:%s/class\s*=\s*\zs\(['"]\)\%(\1\@!.\)*_\(\1\@!.\)*\1/\=substitute(submatch(0),'_',
'-', 'g')/gi

This finds the quoted attribute string containing underscores for
classes and then makes that text available to as "submatch(0)" which
can then be passed to the substitute() function.

-tim



--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment