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

Re: Regex matching the first occurrence of a string

Expand Messages
  • Tim Chase
    ... Without details of what Doesn t work means, it s a little hard to guess. Can your blocks be nested? Begin stuff Begin stuff2 stuff3 End stuff4 End Does
    Message 1 of 6 , May 29 4:25 AM
      On 05/28/2010 09:50 PM, ShayAllen wrote:
      >
      > I want to match everything between and including Begin and End. There are
      > several blocks in the file:
      >
      > Begin
      > ssss
      > dfsdfsdf
      > sdfsdf
      > End
      >
      > Begin
      > sasdfsa
      > sdfgsd
      > hghfgh
      > End
      >
      > My best guess so far:
      > :%s/Begin\_.*\(End\)\{-1}/This was one block/
      > Doesn't work

      Without details of what "Doesn't work" means, it's a little hard
      to guess. Can your blocks be nested?

      Begin
      stuff
      Begin
      stuff2
      stuff3
      End
      stuff4
      End

      Does case matter? Can you have begin/end text in other stuff?

      Begin
      stuff
      this is not the End
      more stuff
      End

      -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
    • Stahlman Family
      ... Try this... %s/^Begin _$ _. {-} _^End$/This was one block/ ... Note that in your pattern, the {-1} was modifying End , which I don t think is what you
      Message 2 of 6 , May 29 4:35 AM
        ShayAllen wrote:
        > I want to match everything between and including Begin and End. There are
        > several blocks in the file:
        >
        > Begin
        > ssss
        > dfsdfsdf
        > sdfsdf
        > End
        >
        > Begin
        > sasdfsa
        > sdfgsd
        > hghfgh
        > End
        >
        > My best guess so far:
        > :%s/Begin\_.*\(End\)\{-1}/This was one block/

        Try this...
        %s/^Begin\_$\_.\{-}\_^End$/This was one block/

        :help /\{
        :help /\_$
        :help /\_^

        Note that in your pattern, the \{-1} was modifying "End", which I don't
        think is what you wanted.

        Brett Stahlman

        > Doesn't work
        >
        > What am I missing?
        > Thanks.

        --
        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
      • ShayAllen
        ... Fair questions, Tim. No nested blocks or other tricky stuff. Brett s solution worked fine and was an instructive example for me. Thanks. -- View this
        Message 3 of 6 , May 30 6:09 PM
          ShayAllen wrote:
          >
          > I want to match everything between and including Begin and End. There are
          > several blocks in the file:
          >
          > Begin
          > ssss
          > dfsdfsdf
          > sdfsdf
          > End
          >
          > Begin
          > sasdfsa
          > sdfgsd
          > hghfgh
          > End
          >
          > My best guess so far:
          > :%s/Begin\_.*\(End\)\{-1}/This was one block/
          > Doesn't work
          >
          > What am I missing?
          > Thanks.
          >

          Fair questions, Tim. No nested blocks or other tricky stuff. Brett's
          solution worked fine and was an instructive example for me. Thanks.

          --
          View this message in context: http://old.nabble.com/Regex-matching-the-first-occurrence-of-a-string-tp28712875p28725924.html
          Sent from the Vim - General mailing list archive at Nabble.com.

          --
          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
        • Ben Fritz
          ... One problem you have is that you use the greedy match, _.*, so that you will match everything between the FIRST begin and the LAST end. Try using
          Message 4 of 6 , May 30 8:02 PM
            On May 28, 9:50 pm, ShayAllen <alexalle...@...> wrote:
            > I want to match everything between and including Begin and End. There are
            > several blocks in the file:
            >
            > Begin
            >   ssss
            >   dfsdfsdf
            >   sdfsdf
            > End
            >
            > Begin
            >   sasdfsa
            >   sdfgsd
            >   hghfgh
            > End
            >
            > My best guess so far:
            > :%s/Begin\_.*\(End\)\{-1}/This was one block/
            > Doesn't work
            >

            One problem you have is that you use the "greedy" match, \_.*, so that
            you will match everything between the FIRST begin and the LAST end.

            Try using \_.\{-} instead, to match in a "non-greedy" fashion, so that
            you only get the individual blocks.

            From your title, I infer that you may want to match only the very
            first block in the file. The easiest way to do this is probably just
            to add the 'c' flag to the end of your substitute command, so that you
            need to "confirm" each replacement (you could then cancel the
            substitute after the first replacement).

            So, your final command would be:

            :%s/Begin\_.*\(End\)\{-1}/This was one block/c

            --
            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
          • Ben Fritz
            ... Oops, ... -- 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
            Message 5 of 6 , May 30 8:02 PM
              On May 30, 10:02 pm, Ben Fritz <fritzophre...@...> wrote:
              >
              > So, your final command would be:
              >
              > :%s/Begin\_.*\(End\)\{-1}/This was one block/c

              Oops,

              :%s/Begin\_.\{-}\(End\)\{-1}/This was one block/c

              --
              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
            Your message has been successfully submitted and would be delivered to recipients shortly.