http://www.perlmonks.org?node_id=950852


in reply to Re: How do I extract named variable names from regex string
in thread How do I extract named variable names from regex string

Sorry for nitpicking, but in praxis this only works with a string where all groups match, "" isn't enough.

use 5.010; "" =~ /(?<mon>\w+)\s(?<mon>\w+)/; print scalar keys %-; # 0

Now constructing such a string is in general even more difficult than just parsing for named captures labels.

Good idea anyway! :)

Cheers Rolf

Replies are listed 'Best First'.
Re^3: How do I extract named variable names from regex string
by JavaFan (Canon) on Jan 31, 2012 at 02:33 UTC
    Thank you for playing, but you fail.

    "" =~ /(?:(?<mon>\w+)\s(?<mon>\w+))?/; say scalar keys %-; # 1
    All that's required is for the pattern to match. Taking the pattern, and wrapping it inside a (?: )? will make it match against "" (except for some degenerate cases). If you look back at my code, this is exactly what I did.
      Ups ... you're right!

      Cheers Rolf