Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^5: Regex Optional capture doesn't (updated)

by NetWallah (Canon)
on Oct 05, 2017 at 17:40 UTC ( [id://1200751]=note: print w/replies, xml ) Need Help??


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

Ok - your explanation makes sense... Thanks (++).

I'll wait to see of other monks can find a way to do what I need with a single regex.

                All power corrupts, but we need electricity.

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

Replies are listed 'Best First'.
Re^6: Regex Optional capture doesn't (updated)
by LanX (Saint) on Oct 05, 2017 at 17:47 UTC
    > can find a way to do what I need with a single regex.

    "single regex" is relative in Perl since you can include Perl-code investigating a sub-match

    Does it must be a single regex, or is it just academic curiosity?

    edit

    you need a positive condition which allows to stop the non-greedy only if you have a match or you reached the end.

    DB<113> print join "|",'a<b1<b2' =~ /(a).+?(?:(c.)|$)/ a| DB<114> print join "|",'a<b1<c2' =~ /(a).+?(?:(c.)|$)/ a|c2 DB<115> print join "|",'a<c1<c2' =~ /(a).+?(?:(c.)|$)/ a|c1

    Try to apply this technique.

    HTH

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

      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.

        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!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1200751]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-03-30 02:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found