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

Regular Expressions for HTML tags??

Expand Messages
  • Hopkins, Jason (GE Healthcare)
    Is there a regex that will match the first tags but not the last in the following snip? blah blah blah blah blah blah more blah
    Message 1 of 4 , Nov 2 12:37 PM
      Is there a regex that will match the first tags but not the last in the
      following snip?

      <this>blah blah <not this>blah
      blah blah blah
      more <this> blah blah<not_this>

      /<[^>]*> will select all the tags, but I only want the first ones.

      Also, is there a way to group so I could replace the first tag with the
      second tag? Something like the following would be cool...

      :%s::\2 \1:



      Thanks,
      Jason
    • Halim, Salman
      If the tags are of the form (closing tag has a slash but is the same tag as the opening, then how about ][^ ]* -- match an opening
      Message 2 of 4 , Nov 2 12:42 PM
        If the tags are of the form <asf></asf> (closing tag has a slash but is
        the same tag as the opening, then how about <[^/>][^>]*> -- match an
        opening <, any character other than a / and > and any number of non->
        characters followed by a closing >.

        Salman.

        > -----Original Message-----
        > From: Hopkins, Jason (GE Healthcare)
        > [mailto:Jason.Hopkins@...]
        > Sent: Wednesday, November 02, 2005 3:37 PM
        > To: vim@...
        > Subject: Regular Expressions for HTML tags??
        >
        > Is there a regex that will match the first tags but not the
        > last in the following snip?
        >
        > <this>blah blah <not this>blah
        > blah blah blah
        > more <this> blah blah<not_this>
        >
        > /<[^>]*> will select all the tags, but I only want the first ones.
        >
        > Also, is there a way to group so I could replace the first
        > tag with the second tag? Something like the following would
        > be cool...
        >
        > :%s::\2 \1:
        >
        >
        >
        > Thanks,
        > Jason
        >
      • Alan G Isaac
        ... / ... Always on one line with that pattern? ... hth, Alan Isaac
        Message 3 of 4 , Nov 2 12:57 PM
          On Wed, 2 Nov 2005, Jason (GE Healthcare) Hopkins apparently wrote:
          > Is there a regex that will match the first tags but not the last in the
          > following snip?
          > <this>blah blah <not this>blah
          > blah blah blah
          > more <this> blah blah<not_this>

          /<\a\+>

          > Also, is there a way to group so I could replace the first
          > tag with the second tag? Something like the following
          > would be cool...
          > :%s::\2 \1:

          Always on one line with that pattern?
          Then:
          :%s/<\a\+>\([^<]\+\)\(<[^>]\+>\)/\1\2\1

          hth,
          Alan Isaac
        • Tim Chase
          ... I think you may have gotten your 1 and 2 backwards... ... ...but otherwise spot on. Using the 2 1 2 yields blah blah blah blah blah
          Message 4 of 4 , Nov 2 2:53 PM
            >>Also, is there a way to group so I could replace the first
            >>tag with the second tag? Something like the following
            >>would be cool...
            >>:%s::\2 \1:
            >
            > Always on one line with that pattern?
            > Then:
            > :%s/<\a\+>\([^<]\+\)\(<[^>]\+>\)/\1\2\1

            I think you may have gotten your \1 and \2 backwards...

            :%s/<\a\+>\([^<]\+\)\(<[^>]\+>\)/\2\1\2

            ...but otherwise spot on. Using the \2\1\2 yields

            <not this>blah blah <not this>blah
            blah blah blah
            more <not_this> blah blah<not_this>

            which sounds like what the OP wanted.

            -tim
          Your message has been successfully submitted and would be delivered to recipients shortly.