Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Regex Optional capture doesn't

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


in reply to Regex Optional capture doesn't

Hi NetWallah,

Can't you use two separate regexes, one for the type and one for the "TagIwant"? This would be so much simpler.

Replies are listed 'Best First'.
Re^2: Regex Optional capture doesn't
by NetWallah (Canon) on Oct 05, 2017 at 17:53 UTC
    I agree - BUT - I have invested a couple of hours in this, and now it has become an obsessive challenge.

    Looking for relief before checking myself into rehab.

    Thanks.

                    All power corrupts, but we need electricity.

      I saw your response soon after you posted it, but had to go to a meeting outside and did not have time. Coming back home, I tried a couple of things and it turns out to be more difficult than I thought.

      Although this solution is far from elegant, it seems to work:

      DB<1> $x = q|<blah1 phase="2" type="MyType" more_keys="Values" <Unwa +nted/> <SomeTagIwant><k1="v1"></SomeTagIwant>|; DB<2> $y = q|<blah1 phase="2" type="MyType" more_keys="Values" <Unwa +nted/> <SomeTagIDontWant><k1="v1"></SomeTagIDontWant>|; DB<3> print "$_ " for $x =~ /type="([^"]+)".+?(\w+?TagIwant) | typ +e="([^"]+)"/x; MyType SomeTagIwant DB<4> print "$_ " for $y =~ /type="([^"]+)".+?(\\w+?TagIwant) | ty +pe="([^"]+)"/x;; MyType DB<5>
      It could be slightly improved with a named regex:
      DB<6> $type = qr/type="([^"]+)"/; DB<7> print "$_ " for $x =~ /$type .+?(\w+?TagIwant) | $type /x; MyType SomeTagIwant DB<8> print "$_ " for $y =~ /$type .+?(\w+?TagIwant) | $type /x; MyType
      Well, I think that haukex's solution below looks better.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (11)
As of 2024-04-18 10:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found