in reply to REGEX Match Single Digit

YAPE::Regex::Explain is a handy tool to help you understand regular expressions:
use strict; use warnings; use YAPE::Regex::Explain; print YAPE::Regex::Explain->new('^\d[1]{1}$')->explain(); __END__ The regular expression: (?-imsx:^\d[1]{1}$) 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): ---------------------------------------------------------------------- ^ the beginning of the string ---------------------------------------------------------------------- \d digits (0-9) ---------------------------------------------------------------------- [1]{1} any character of: '1' (1 times) ---------------------------------------------------------------------- $ before an optional \n, and the end of the string ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------

Replies are listed 'Best First'.
Re^2: REGEX Match Single Digit
by spickles (Scribe) on Aug 28, 2009 at 16:10 UTC
    That's cool. Thanks. From that output, am I understanding that one of my original attempts should have worked?
      /\d1{1}/ matches any single decimal digit, "1" included, FOLLOWED BY a single "1".

      toolic's YAPE explanation makes that clear, but your original spec indicates you want a single digit (known in advance, I gather).

      Hence, /1{1}/ for a single "1" anywhere in a line, or /^1{1}$ for a line with a single "1" as its only content.

      No