Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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'


In reply to Re^2: Pattern matching exlusion by AnomalousMonk
in thread Pattern matching exlusion by wrkrbeee

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-24 19:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found