in reply to a regex pattern how to understand

The [[:alpha:] ] matches alphabetic characters plus a space character

Correct,

so I think "\s+Age:" should match one or more space and "Age:"

correct,

There is only one space between name and age.

and correct. And the code you show produces the output you show.

If you want to understand what the regex is doing, you can look at the gory details of how Perl handles it by adding use re 'debug'; at the top of your code. Or, have a look at https://regex101.com/r/pP4dN2/1

Otherwise, I don't understand what exactly you are asking?

Replies are listed 'Best First'.
Re^2: a regex pattern how to understand
by Thai Heng (Beadle) on Jul 08, 2015 at 22:54 UTC
    The space matched in the former [[:alpha:] ], and can't matched in the last \s+Age:。So I think the text can't matched the reg pattern. Because there is only one space between name and age.
      The space matched in the former [[:alpha:] ], and can't matched in the last \s+Age:。So I think the text can't matched the reg pattern. Because there is only one space between name and age.

      Hmm, if perl matches it, why do you think that is?

      If you turn on use re 'debug' ; what do you see?

      I see

      149 <Princ> <e Age: 53> | 21: CLOSE1(23) 149 <Princ> <e Age: 53> | 23: PLUS(25) SPACE can match 0 times out of 2 +147483647... failed... ANYOF[ A-Za-z][{unicode}+utf8::XPo +sixAlpha 00AA 00B5 00BA 00C0-00D6 00D8-00F6 00F8- 02C1] can match 1 times out of 1... 150 <rince> < Age: 53%n> | 21: CLOSE1(23) 150 <rince> < Age: 53%n> | 23: PLUS(25) SPACE can match 1 times out of 2 +147483647... 151 <ince > <Age: 53%nO> | 25: EXACT <Age:>(27) 155 < Age:> < 53%nOccup> | 27: PLUS(29) SPACE can match 1 times out of + 2147483647...

      +? means match the least amount possible, the least amount doesn't include space, because the next pattern wants space

      As the other anon already said, the +? modifier makes the expression non-greedy, meaning it doesn't consume all possible characters. Even if the ? is removed, the regular expression still works due to Backtracking:

      For a regular expression to match, the entire regular expression must match, not just part of it. So if the beginning of a pattern containing a quantifier succeeds in a way that causes later parts in the pattern to fail, the matching engine backs up and recalculates the beginning part--that's why it's called backtracking.