Saturday, March 2, 2013

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

Hi,

I'd like to change all the underscores within a class="____" to hyphens. For instance, I have:

<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.

I thought maybe I could use a lookbehind for this, but am still having trouble. I understand why the following doesn't work (the /g tries to match the `class="` again and fails), but it's as close as I've gotten.

s/\(class="\)\@<=\([^"_]*\)_/\2-/g

Can anyone offer regex help? Or an alternative method? Something akin to g/pattern/action except instead of limiting the action to be taken on lines that match, it's taken only within the matching area, so I could do something like:

:magicaction/class="[^"]"/s/_/-/g

Thanks in advance!

Hsiu

--
--
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