in reply to Re^4: Perl program to look into the phone directory and in case of a match, print the name along with the number
in thread Perl program to look into the phone directory and in case of a match, print the name along with the number

Hi dk27,

when you used the operator "?:", this is used to form a non-capturing group so that \s+ is explicitly not captured while checking for regex and this makes it a match to the last test case and matches that for execution right?

Both (...) and (?:...) group regex patterns, so both /ab(cd)?/ and /ab(?:cd)?/ mean "match ab, optionally followed by cd", and both /ab(cd|ef)gh/ and /ab(?:cd|ef)gh/ mean "match abcdgh or abefgh". The difference between the two is that capturing groups (...) will populate the $1, $2, etc. variables, while non-capturing groups (?:...) will not populate those variables, so the latter are typically used for grouping only.

Here is how I read the regex I showed, /^\s*([a-zA-Z]+)(?:\s+(\d+))?/:

  1. ^: Anchor at beginning of string (Update: Note that if the /m modifier were in use, this would anchor to the beginning of any line within the string.)
  2. \s*: Zero or more whitespace characters
  3. ([a-zA-Z]+): One or more letters, storing the match in $1 (because this is the first set of capturing parentheses)
  4. (?:...)?: Optionally match the following:
    1. \s+: One or more whitespace characters
    2. (\d+): One or more digits, storing the result in $2 (because this is the second set of capturing parentheses)

If I had used (...) instead of (?:...), the match would be exactly the same, the difference would be that $2 would be populated with the match of (\s+(\d+)), and $3 would be populated with the match of (\d+). One would have had to write ($name, undef, $phone) = $line =~/^\s*([a-zA-Z]+)(\s+(\d+))?/, which is possible too, but is simply a bit of a waste.

For the documentation see perlretut, perlrequick, and perlre.

(As a side note, Perl v5.22 introduced the /n modifier, which turns all (...) in the regular expression into non-capturing groups.)

Hope this helps,
-- Hauke D

  • Comment on Re^5: Perl program to look into the phone directory and in case of a match, print the name along with the number
  • Select or Download Code