user3048179
user3048179

Reputation: 51

Why does this regex fail to work...any ideas?

I am faced with strings as follows:

I am nearly certain I have translated this into the following regex correctly but this line of PHP code still returns NULL when passed valid strings. Furthermore, when I test this regex with regexpal and the identical subject string, the correct result is returned. I'm pretty sure I'm having a problem with the pattern delimiter or the first 2 groups (start of line then character check). Any ideas? - Brandon

preg_match_all('&^(\C|\M|\P|\T|\K|\X|\Q)[A-Z0-9]{3}.*\sM?[0-9]{2}/M?[0-9]{2,3}\s&', $subject, $resultArr);

Upvotes: 2

Views: 69

Answers (1)

Mike Brant
Mike Brant

Reputation: 71384

First, I would typically suggest using a more common pattern delimiter such as /, #, or ~. I personally would not actually use / here since you use that in the pattern. This is just preference though, & is totally valid.

Second, there is no need for backslashes along with the characters at the start of the line (you can also use a character class for these, which I find more readable). As shown, some of these do form valid escape sequences, so you are likely getting unpredictable behavior.

Third, I am guessing you want an ungreedy search (U pattern modifier after pattern). I find in most cases this is desired behavior when using .* somewhere in pattern. In this case, since you are using preg_match_all() a greedy search is particularly problematic, as it would match the first case where the first portion of your pattern matches along with the last case with the last part of the pattern matches with all other potential matches lumped into the .* portion of the pattern.

So this leaves us with something like this:

$pattern = '#^[CMPTKXQ][A-Z0-9]{3}.*\sM?[0-9]{2}/M?[0-9]{2,3}\s#U';
preg_match_all($pattern, $subject, $resultArr);

Upvotes: 2

Related Questions