1

I have a text and I want to use a regex pattern to put the text into groups.

Here is an example of text I could have:

Some text...

<style id=1>Header</style>

Some other text...

<style id=2>
- Bullet 1
- Bullet 2
</style>

Trailing text...

Requirements:

  1. I may have, or not, text before or after the tag
  2. I want a pattern that returns multiple matches. Ex: In that case, I could have 2 or 3 matches depending on the regex pattern groups.
  3. I may have line breaks

I'm currently using this pattern:

(?<prefix>[\s\S]*?)<style id=(?<id>[0-9]+)>(?<content>[\s\S]*?)</style>

Currently, I have 2 matches using this pattern, which are:

[prefix]: Some text...
[id]: 1
[content]: Header

[prefix]: Some other text...
[id]: 2
[content]:
- Bullet 1
- Bullet 2

I'm not able to create a group that will catch the "Trailing text...". I tried but when I add a group at the end of my pattern, it returns only one match and its prefix include the first tag.

Any idea??

Thanks

MaxD
  • 51
  • 6

1 Answers1

0

You can repeat the whole pattern with a * and then add something that matches everything.

((?<prefix>[\s\S]*?)<style id=(?<id>[0-9]+)>(?<content>[\s\S]*?)</style>)*(?<suffix>[\s\S]*)
dogiordano
  • 691
  • 6
  • 13