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 ----------------------------------------------------------------------

-- Ken


In reply to Re: a regex pattern how to understand by kcott
in thread a regex pattern how to understand by Thai Heng

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.