Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: To not capture recursive group while collecting certain matches

by Anonymous Monk
on Nov 29, 2021 at 16:31 UTC ( #11139226=note: print w/replies, xml ) Need Help??


in reply to To not capture recursive group while collecting certain matches

As given, your example does not match anything. If you use re 'debug'; you will get a fairly cryptic trace of what the regular expression is doing:

$ perl -Mre=debug -E 'my @m='lkjkljkjlkhkjkfjkfkvklkv'=~/^\w*(kl(?1)\w*).*?(kv)/g; say for @m;'
Compiling REx "^\w*(kl(?1)\w*).*?(kv)"
Final program:
   1: SBOL /^/ (2)
   2: STAR (4)
   3:   POSIXU\w (0)
   4: OPEN1 (6)
   6:   EXACT <kl> (8)
   8:   GOSUB1-4:4 (11)
  11:   STAR (13)
  12:     POSIXU\w (0)
  13: CLOSE1 (15)
  15: MINMOD (16)
  16: STAR (18)
  17:   REG_ANY (0)
  18: OPEN2 (20)
  20:   EXACT <kv> (22)
  22: CLOSE2 (24)
  24: END (0)
floating "klkl" at 0..9223372036854775807 (checking floating) anchored(SBOL) minlen 6 
Matching REx "^\w*(kl(?1)\w*).*?(kv)" against "lkjkljkjlkhkjkfjkfkvklkv"
Intuit: trying to determine minimum start position...
  doing 'check' fbm scan, 0..22 gave -1
  Did not find floating substr "klkl"...
Match rejected by optimizer
Freeing REx: "^\w*(kl(?1)\w*).*?(kv)"

The trace says your match never got started because the string being matched does not contain 'klkl'. The first 'kl' is the literal one from your regular expression; the second is because of the recursion. I believe that even if the string contained 'klkl' there would be no match, because there is no way for the recursion to end; it would just look for 'klklkl', then 'klklklkl', and so on.

Can you say what you expect @m to contain in your example?

And can you provide an example where you match against the shortest possible string that you want to match successfully?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (None)
    As of 2022-01-26 06:13 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      In 2022, my preferred method to securely store passwords is:












      Results (69 votes). Check out past polls.

      Notices?