Loading ...
Sorry, an error occurred while loading the content.

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

Expand Messages
  • John Little
    Using zs can be much simpler than fancy assertions and captures. ... Repeat until pattern not found. Regards, John Little -- -- You received this message from
    Message 1 of 4 , Mar 3 2:12 AM
    • 0 Attachment
      Using \zs can be much simpler than fancy assertions and captures.

      :%s/class="[^"_]*\zs_/-/g

      Repeat until pattern not found.

      Regards, John Little

      --
      --
      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@....
      For more options, visit https://groups.google.com/groups/opt_out.
    • Christian Brabandt
      Hi Hsiu! ... That is pretty close. This should work: s/ (class= %([^ ]* ) ) @
      Message 2 of 4 , Mar 3 4:37 AM
      • 0 Attachment
        Hi Hsiu!

        On Sa, 02 Mär 2013, Hsiu Dai wrote:

        > 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

        That is pretty close. This should work:
        s/\(class="\%([^"]*\)\)\@<=_/-/g

        regards,
        Christian
        --
        Hätte Gott gewollt, daß wir denken, so hätte er uns ein Gehirn gegeben.
        Und hätte Gott gewollt, daß wir fliegen, hätte er uns Flugtickets gegeben.

        --
        --
        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@....
        For more options, visit https://groups.google.com/groups/opt_out.
      Your message has been successfully submitted and would be delivered to recipients shortly.