Reputation: 21
I need to write regular expression that will match any pair of tags <(.*?)>.*?</\1>
but only if there is no other pair tags between them. Tag names are variable length.
Upvotes: 2
Views: 309
Reputation: 21999
You can easily exclude nested tags by excluding the angle bracket needed to open them:
<([^<>]+)>[^<]*</\1>
This regex won't work if the opening tag has attributes. If you want to allow those, try this:
<(\S+)[^<>]*>[^<]*</\1>
Upvotes: 3
Reputation: 2669
You simply should not do this with regex. However, don't take my word for it.
Upvotes: 1
Reputation: 3701
So long as you maintain the "Only if there is no other tags between them" this is easy.
<\s*([^>]+?)\s*>[^<]*</\s*\1\s*>
Upvotes: 0