in reply to Quick Regex question

Also consider that a "channel" number with more than four digits will produce a match:

c:\@Work\Perl\monks>perl -wMstrict -le "my $text = shift; print qq{text '$text'}; ;; my $match = my ($band, $channel) = $text =~ /Signal LTE-FDD (\d{1,2}) +; Channel (\d{3,4})/i; ;; if ($match) { print qq{band '$band' channel '$channel'}; } else { print 'no match'; } " "Signal LTE-FDD 45; Channel 123456789" text 'Signal LTE-FDD 45; Channel 123456789' band '45' channel '1234'
You may want to use the  \A \z string beginning/end assertions or a  (?!\d) negative look-ahead to limit such matches. See Assertions and Look-Around Assertions in perlre.

Update: Note also that use of the  $& regex special variable or its "friends"  $` $' anywhere in a script imposes significant regex matching performance penalties everywhere in the script. See perlvar.


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^2: Quick Regex question
by Laurent_R (Canon) on Oct 30, 2015 at 09:31 UTC
    Note also that use of the $& regex special variable or its "friends" $` $' anywhere in a script imposes significant regex matching performance penalties everywhere in the script.
    Yeah, this used to be true, but has been slightly improved overtime, and has been basically fixed with Perl 5.20. So the penalty may or may not be there depending of the Perl version being used.

    Having said that, I've personally never used those variables for anything else than just testing a couple of times what they do (if only because, at least at work, I'm stuck with a much older version of Perl).