The problem is that .*
is greedy. Take a look at this pattern /foo.*bar/
and the following text
foo hello bar world bar
The .*
would match hello bar world
because of the last bar
.
To change this behaviour, you can simply add a ?
, so the pattern would look like this
|<.*?bentleysoft-widget="([a-z]+)"></.*?>|
and it should work
Also since you want to capture the right closing tag, you should use a backreference here
|<(\S+?).*?bentleysoft-widget="([a-z]+)"></\\1>|
(\S+?)
will get you the correct tag name (because \S
matches everything but whitespaces), which can be backreferenced via \\1
(1 because it is the first group).