Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^2: Pattern matching exlusion

by AnomalousMonk (Archbishop)
on Nov 29, 2014 at 04:53 UTC ( [id://1108695]=note: print w/replies, xml ) Need Help??


in reply to Re: Pattern matching exlusion
in thread Pattern matching exlusion

my $formget = qr(10-[KQ][A-Z0-9]*(?!/))i;

The problem with this regex when used in a  /^$formget/ match is that it allows strings like  10-KA/ to match: if a  / is found at the end of  10-KA/ the regex can backtrack to  10-K and look ahead to  A which is not a  / character! BrowserUk's approach below gets around this by using an end-of-string anchor assertion to make sure that only non-/ characters follow the [KQ]. If anchoring were not possible, another way would be to use an 'atomic'  (?>pattern) group which will not allow backtracking into it:

c:\@Work\Perl\monks>perl -wMstrict -le "my @tests = qw[ 10-K 10-KSB 10-K405 10-KSB405 10-Q 10-K/B 10-KSB/ABC 10-K405/A 10-KSB405/A 10-Q/A 10-KA/ ]; ;; my $formget = qr{ (?i) 10- [KQ] [A-Z0-9]* (?! /) }xms; printf 'valid: '; /^$formget/ and printf qq{'$_' } for @tests; print ''; ;; my $formget2 = qr{ (?> (?i) 10- [KQ] [A-Z0-9]*) (?! /) }xms; printf 'valid2: '; /^$formget2/ and printf qq{'$_' } for @tests; " valid: '10-K' '10-KSB' '10-K405' '10-KSB405' '10-Q' '10-KSB/ABC' '10- +K405/A' '10-KSB405/A' '10-KA/' valid2: '10-K' '10-KSB' '10-K405' '10-KSB405' '10-Q'

Replies are listed 'Best First'.
Re^3: Pattern matching exlusion
by wrkrbeee (Scribe) on Nov 29, 2014 at 13:44 UTC
    Thank you AnomalousMonk, that makes sense. I really appreciate your time and interest. Have a great weekend!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1108695]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (7)
As of 2024-04-19 08:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found