in reply to Matching dialplan rules...

Definitely when writing a complex one like this, use the /x modifier to make things clearer to yourself. Taking your expression as is:

$pattern =~ / ^ _* # 0 or more underscores to start ( (\d*|#*|\**)+ # Any combo of digits, #s, or *s (\[\d+\-\d\])? # Maybe a [x-y] ) (X*\.?\!?) # Zero or more Xs, and maybe a . or ! $ /gx;

The other thing I'd recommend doing is writing a test script to make sure your regex works.

use strict; use warnings; use Test::More qw(no_plan); my $regex = qr{ ... stuff ... }x; # Tests like( '100', $regex, 'Valid numeric' ); like( '_100', $regex, 'Valid numeric with leading underscore' ); # etc. unlike( '', $regex, 'Invalid empty string' ); # etc.

Replies are listed 'Best First'.
Re^2: Matching dialplan rules...
by Anonymous Monk on Dec 04, 2008 at 14:49 UTC
    #!/usr/bin/perl -- use YAPE::Regex::Explain; print YAPE::Regex::Explain->new( qr/^_*((\d*|#*|\**)+(\[\d+\-\d\])?)(X +*\.?\!?)$/ )->explain; __END__ The regular expression: (?-imsx:^_*((\d*|#*|\**)+(\[\d+\-\d\])?)(X*\.?!?)$) 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 ---------------------------------------------------------------------- _* '_' (0 or more times (matching the most amount possible)) ---------------------------------------------------------------------- ( group and capture to \1: ---------------------------------------------------------------------- ( group and capture to \2 (1 or more times (matching the most amount possible)): ---------------------------------------------------------------------- \d* digits (0-9) (0 or more times (matching the most amount possible)) ---------------------------------------------------------------------- | OR ---------------------------------------------------------------------- #* '#' (0 or more times (matching the most amount possible)) ---------------------------------------------------------------------- | OR ---------------------------------------------------------------------- \** '*' (0 or more times (matching the most amount possible)) ---------------------------------------------------------------------- )+ end of \2 (NOTE: because you're using a quantifier on this capture, only the LAST repetition of the captured pattern will be stored in \2) ---------------------------------------------------------------------- ( group and capture to \3 (optional (matching the most amount possible)): ---------------------------------------------------------------------- \[ '[' ---------------------------------------------------------------------- \d+ digits (0-9) (1 or more times (matching the most amount possible)) ---------------------------------------------------------------------- \- '-' ---------------------------------------------------------------------- \d digits (0-9) ---------------------------------------------------------------------- \] ']' ---------------------------------------------------------------------- )? end of \3 (NOTE: because you're using a quantifier on this capture, only the LAST repetition of the captured pattern will be stored in \3) ---------------------------------------------------------------------- ) end of \1 ---------------------------------------------------------------------- ( group and capture to \4: ---------------------------------------------------------------------- X* 'X' (0 or more times (matching the most amount possible)) ---------------------------------------------------------------------- \.? '.' (optional (matching the most amount possible)) ---------------------------------------------------------------------- !? '!' (optional (matching the most amount possible)) ---------------------------------------------------------------------- ) end of \4 ---------------------------------------------------------------------- $ before an optional \n, and the end of the string ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------