Regular Expressions for HTML tags??

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Regular Expressions for HTML tags??

Hopkins, Jason (GE Healthcare)
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  
Reply | Threaded
Open this post in threaded view
|

RE: Regular Expressions for HTML tags??

Halim, Salman
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:[hidden email]]
> Sent: Wednesday, November 02, 2005 3:37 PM
> To: [hidden email]
> 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  
>
Reply | Threaded
Open this post in threaded view
|

Re: Regular Expressions for HTML tags??

Alan G Isaac
In reply to this post by Hopkins, Jason (GE Healthcare)
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




Reply | Threaded
Open this post in threaded view
|

Re: Regular Expressions for HTML tags??

Tim Chase-2
>>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