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'
-
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.
|