Reputation: 2922
I'm trying to understand more about look-around asserts and I found this thread, where their solution is supposed to work in some engines but not PHP's, because of PHP's requiring the look-behind assert to be fixed-length.
What I want is to make the same scenario work in PHP or at least know if it's possible at all.
I tried to reduce the regex rules explanation, so it's not the same as in the thread I mention above, but it follows the same principle.
Need to match a string built in three parts:
So, these would match:
But these would NOT match:
I've been trying with some variations of the regex pattern below but it's not working - this particular case because of the fixed-length limitation:
.*(?<!abc-[\d-]{3,5})\.htm[^l]?$
I also used different test strings and forgot about the 3-5 range part, focusing only on exactly , say, 3 numbers and/or hyphens, and used the regex below, and it still doesn't work, which is why I decided to ask for help on this:
.*(?<!abc-[\d-]{3})\.htm[^l]?$
Could anyone of you regex gurus help me out here?
Edit
This is my testing PHP code:
$regex = "/^(?!.*abc-[\d-]{3,5})[a-zA-Z0-9-]+\.html?$/";
foreach ( $matching2 as $k => $v ) {
$matches = preg_match( $regex, $v );
echo '"', $v, '"', ( $matches != 0 ) ? ' matches' : ' doesn\'t match', '<br />';
}
Upvotes: 1
Views: 554
Reputation: 43683
You probably looking for regex pattern
^(?!.*abc-[\d-]{3,5}[^\d-])[A-Za-z0-9].*[.]html?$
Upvotes: 1
Reputation: 44279
Why do you need to need to look at that in reverse? Why not just use a lookahead?
^(?!.*abc-[\d-]{3,5}[^\d-])[a-zA-Z0-9-]+\.html?$
This will simply start looking at the beginning of the string and the lookahead tries to find the disallowed string anywhere (.*
) in the string. If it does, the lookahead makes the pattern fail. This also include the requirement, that the string consists only of alphanumerics and hyphens.
This is by the way the same solution that is used for the question you linked. Perl cannot cope with variable-length lookbehinds either. Only .NET can.
Another note: if you ever encounter an example where you actually do need a variable-length lookbehind (but not a variable-length lookahead)... reverse the string (and the pattern, too, of course). ;)
Upvotes: 2