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

More like a perverse obsession.

Thanks for your tip on "use re 'debug'".

It shows that the RE gets confounded by the <Unwanted> tag.
Removing that tag allows the regex to capture both items I want.

However, this is not an option while I'm parsing the log.

                All power corrupts, but we need electricity.

  • Comment on Re^7: Regex Optional capture doesn't (updated)

Replies are listed 'Best First'.
Re^8: Regex Optional capture doesn't (solved !)
by LanX (Saint) on Oct 05, 2017 at 18:07 UTC
    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!

      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.