Search code examples
regexperltypescapturing-group

In Perl, how many groups are in the matched regex?


I would like to tell the difference between a number 1 and string '1'.

The reason that I want to do this is because I want to determine the number of capturing parentheses in a regular expression after a successful match. According the perlop doc, a list (1) is returned when there are no capturing groups in the pattern. So if I get a successful match and a list (1) then I cannot tell if the pattern has no parens or it has one paren and it matched a '1'. I can resolve that ambiguity if there is a difference between number 1 and string '1'.


Solution

  • For example, bitwise operators behave differently for strings and integers:

    ~1 = 18446744073709551614

    ~'1' = Î ('1' = 0x31, ~'1' = ~0x31 = 0xce = 'Î')

    #!/usr/bin/perl
    
    ($b) = ('1' =~ /(1)/);
    print isstring($b) ? "string\n" : "int\n";
    ($b) = ('1' =~ /1/);
    print isstring($b) ? "string\n" : "int\n";
    
    sub isstring() {
        return ($_[0] & ~$_[0]);
    }
    

    isstring returns either 0 (as a result of numeric bitwise op) which is false, or "\0" (as a result of bitwise string ops, set perldoc perlop) which is true as it is a non-empty string.