G'day Thai Heng,
Perhaps your wording, "alphabetic characters plus a space",
is a clue to what you're not understanding.
I probably would have said "alphabetic characters or a space".
Some documentation that may help you:
There are also some tools that can help you to understand regex patterns.
I see you've already been shown "use re 'debug';": you may have found the output somewhat esoteric.
Here's a couple more that may be more suitable.
Damian Conway's Regexp::Debugger
is a particular favourite of mine.
It provides a dynamic explanation of each step that the regex engine performs.
It's output shows: the position in the string and the part of the regex trying to match at this position;
what's been matched so far; what values $1, $2, etc. currently hold; and so on.
The simplest usage is to just add use Regexp::Debugger; near the top of your code;
run your code; and use 's' to step through each operation.
See the documentation for more commands and other information.
YAPE::Regex::Explain is easy to use.
It provides a static explanation of the regex you give it.
While you're learning, and using regexes such as you have here, this should be fine;
when you start looking at newer, more advanced regex constructs, this module won't help you
(see its LIMITATIONS
section for more details).
#!/usr/bin/env perl
use strict;
use warnings;
use YAPE::Regex::Explain;
my $re = qr{Name:\s+([[:alpha:] ]+?)\s+Age:\s+(\d+)};
print YAPE::Regex::Explain::->new($re)->explain;
Output:
The regular expression:
(?-imsx:Name:\s+([[:alpha:] ]+?)\s+Age:\s+(\d+))
matches as follows:
NODE EXPLANATION
----------------------------------------------------------------------
(?-imsx: group, but do not capture (case-sensitive)
(with ^ and $ matching normally) (with . not
matching \n) (matching whitespace and #
normally):
----------------------------------------------------------------------
Name: 'Name:'
----------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1 or
more times (matching the most amount
possible))
----------------------------------------------------------------------
( group and capture to \1:
----------------------------------------------------------------------
[[:alpha:] ]+? any character of: letters, ' ' (1 or
more times (matching the least amount
possible))
----------------------------------------------------------------------
) end of \1
----------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1 or
more times (matching the most amount
possible))
----------------------------------------------------------------------
Age: 'Age:'
----------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1 or
more times (matching the most amount
possible))
----------------------------------------------------------------------
( group and capture to \2:
----------------------------------------------------------------------
\d+ digits (0-9) (1 or more times (matching
the most amount possible))
----------------------------------------------------------------------
) end of \2
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
|