in reply to Quick Regex question
Also consider that a "channel" number with more than four digits will produce a match:
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.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'
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 |