1

I have a pattern that findall the tags in this format, <.* :.*> . From nested tags, I am taking only the child tag. Here I need to distinguish the brackets(< and >) from '/<' and '/>'. Is there any way that I can do this in the same pattern itself?

eg : input string

<testing this> any text </<this is not a tag>any text<this will fail/>>

output:

['<testing this>','</<this is not a tag>','<this will fail/>>']

Any suggestion please let me know.

Aprillion
  • 21,510
  • 5
  • 55
  • 89
Sohn
  • 166
  • 3
  • 13

1 Answers1

1

Use this pattern:

(?<!/)<.*?(?<!/)>

The (?<!/) is a negative lookbehind that assures there is no slash directly to the left of each < or > without actually consuming the character.

Check this pattern out on regex101.com

Byte Commander
  • 6,506
  • 6
  • 44
  • 71