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+))?/:
- ^: 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.)
- \s*: Zero or more whitespace characters
- ([a-zA-Z]+): One or more letters, storing the match in $1 (because this is the first set of capturing parentheses)
- (?:...)?: Optionally match the following:
- \s+: One or more whitespace characters
- (\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
| [reply] [d/l] [select] |