in reply to Re^7: Regex Optional capture doesn't (updated)
in thread Regex Optional capture doesn't

my proposition from the edit here works for me with your data:

DB<132> p $x <blah1 phase="2" type="MyType" more_keys="Values" <Unwanted/> <SomeTa +gIwant><k1="v1"></SomeTagIwant> DB<133> $re = qr/\btype="([^"]+)".+?<(\w+TagIwant\b|$)/ DB<134> print join "|", $x =~ $re MyType|SomeTagIwant

update
DB<154> p $x <blah1 phase="2" type="MyType" more_keys="Values" <Unwanted/> <SomeTa +gIwant><k1="v1"></SomeTagIwant> DB<155> p $y <blah1 phase="2" type="MyType" more_keys="Values" <Unwanted/> DB<156> $re = qr/ \btype = "([^"]+)" .+? (?: < ( \w+TagIwant)\b | $ + ) /x DB<157> print join "|", $x =~ $re MyType|SomeTagIwant DB<158> print join "|", $y =~ $re MyType|

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Je suis Charlie!

Replies are listed 'Best First'.
Re^9: Regex Optional capture doesn't (solved ?)
by NetWallah (Canon) on Oct 05, 2017 at 18:33 UTC
    Excellent - your "Update" one works !
    The pre-update fails when the tag is absent.
    I saw this the same time as hauxex's working solution, below .. So I declare both winners !
    Thanks.

                    All power corrupts, but we need electricity.