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

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

Expand Messages
  • Tim Chase
    ... Your guesses were pretty close. The magic resides within ... where you learn you can do things like ... - , g )/gi This finds the quoted attribute
    Message 1 of 4 , Mar 2, 2013
    • 0 Attachment
      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@....
      For more options, visit https://groups.google.com/groups/opt_out.
    • 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 2 of 4 , Mar 3, 2013
      • 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 3 of 4 , Mar 3, 2013
        • 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.