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

replace repeating pattern group

Expand Messages
  • Don M
    Hi, I have the need to convert a xml tag that contains comma separated entries into multiple tags. This is mixed into a larger XML file and the pattern may
    Message 1 of 3 , Sep 30, 2009
    • 0 Attachment
      Hi,

      I have the need to convert a xml tag that contains comma separated
      entries into multiple tags. This is mixed into a larger XML file and
      the pattern may occur multiple times on the same line or on different
      lines.

      For example:

      Replace <tags>one, two, three, four</tags> with:

      <tags>
      <tag>one</tag>
      <tag>two</tag>
      <tag>three</tag>
      <tag>four</tag>
      </tags>

      There won't always be four entries. In fact there could be zero.
      Here's what I did, which works good enough:

      %s/<tags>\(\_[^<]\+\)<\/tags>/\=substitute(submatch(0), ",\s\*", "<\/
      tags>\r<tags>", "g")/g

      My question is: Is there another, non-vim specific way to reference
      the repeating group of comma separated values. What I wanted to do
      was something like this:

      %s/<tags>\(\([^,]\+\),\s*\)\+</tags>/<tag>\1<\tag>/g

      But \1 will only refer to the first time it hits the pattern, not the
      subsequent one. The basic task is to find the content between the
      <tags> tags and replace the comma separated entries with tag
      delineated entries. Ideally using "standard" regex notation that
      might work in vim, perl, and sed so I could script it.

      I tried googling for this, but I don't know what to google for.
      Searching fore regex, repeating group, etc gives back other examples.

      Thanks,
      Don

      However
      --~--~---------~--~----~------------~-------~--~----~
      You received this message from the "vim_use" maillist.
      For more information, visit http://www.vim.org/maillist.php
      -~----------~----~----~----~------~----~------~--~---
    • Paul
      ... Try searching for recursive regular expressions. -- . --~--~---------~--~----~------------~-------~--~----~ You received this message from the vim_use
      Message 2 of 3 , Oct 1, 2009
      • 0 Attachment
        > My question is: Is there another, non-vim specific way to reference
        > the repeating group of comma separated values. What I wanted to do
        > was something like this:

        > I tried googling for this, but I don't know what to google for.
        > Searching fore regex, repeating group, etc gives back other examples.

        Try searching for recursive regular expressions.

        --
        .

        --~--~---------~--~----~------------~-------~--~----~
        You received this message from the "vim_use" maillist.
        For more information, visit http://www.vim.org/maillist.php
        -~----------~----~----~----~------~----~------~--~---
      • Don M
        Thanks for the tip on what to search for. I still haven t found my answer but that tip helps! ... --~--~---------~--~----~------------~-------~--~----~ You
        Message 3 of 3 , Oct 2, 2009
        • 0 Attachment
          Thanks for the tip on what to search for. I still haven't found my
          answer but that tip helps!

          On Oct 1, 4:25 am, Paul <vim5...@...> wrote:
          > > My question is:  Is there another, non-vim specific way to reference
          > > the repeating group of comma separated values.  What I wanted to do
          > > was something like this:
          > > I tried googling for this, but I don't know what to google for.
          > > Searching fore regex, repeating group, etc gives back other examples.
          >
          > Try searching forrecursiveregular expressions.
          >
          > --
          > .
          --~--~---------~--~----~------------~-------~--~----~
          You received this message from the "vim_use" maillist.
          For more information, visit http://www.vim.org/maillist.php
          -~----------~----~----~----~------~----~------~--~---
        Your message has been successfully submitted and would be delivered to recipients shortly.