Hi there
Last Friday, I noticed that our version (3.16.3) now supports PHP's Perl-Compatible Regular Expressions for server side validation of text questions. I thought to myself "WOW!!! That's so cool now I can do stuff like validate phone numbers and do word counts on text boxes! (OK so it doesn't support all the PCRE modifiers available to PHP but…)"
So I created a couple of regular expressions. Tested them directly with a basic PHP script, ironed out the kinks and plugged them into Matrix's "Complex Validation Rules: Regular Expression". Matrix came up with errors every time I put it content despite the fact that a normal preg_match matched them.
The phone number PCRE is
^ (?:\(?(?:\+[0-9]{2}[-\t ][0-9]|0[-\t ][0-9])\)?(?:[-\t ][0-9]){8}|[1-57](?:[-\t ]*[0-9]){3}) $
and in normal PHP (using preg_match) it accurately matches the following: "+61 2 8507 9900, +612 8507 9900, 02 8507 9900, (02) 8507 9900, (02) 85-079-900, 9900" etc. etc.
but not things like "wk 02 8507 9900, 2 8507 9900, 07 9900, 02 8507 9900 dsflj" etc. etc.
likewise my word counter RegEx "^(?:[ \n\t][^- \n\t?!.,():;"']+[- \n\t?!.,():;"']?){0,255}$" happily matches up to 255 words in a normal preg_match but not in Matrix's "Complex Validation Rules: Regular Expression".
What am I (or Matrix) doing wrong?
PS: Why doesn't Matrix support modifiers and user supplied delimiters? This would take some of the unknown out of the RegEx trouble shooting. I'm also sure it would have been much easier to code.
NOTE: After much testing I discovered that alternation using the pipe/vertical bar "|" breaks any regular expression I've tried however this doesn't explain the word counter RegEx not working. Regular Expressions that can't make use of Alternation are fairly pointless.
What are the errors?
There are no system errors. Matrix just doesn't seem to match strings correctly. I think it might be something to do with the fact that "Alternation" doesn't seem to work in Matrix.
I've been going through and testing various RegExs and found that the following work "[font=“Courier New”][0-9]{4}", "[0-9]{8}[/font]" and "font=“Courier New”?([0-9]{4})?[/font]"
but not: "font=“Courier New”?[/font]" or even "font=“Courier New”?[/font]" which more accurately match the same as "font=“Courier New”?([0-9]{4})?[/font]" (which does work)
The only difference between "font=“Courier New”?([0-9]{4})?[/font]" and "font=“Courier New”?[/font]" is the pipe/vertical bar "|" used to denote "alternation".
NOTE: there must also be other things wrong because my other RegEx "[font=“Courier New”]^(?:[ \n\t]*[^- \n\t?!.,():;"']+[- \n\t?!.,():;"']?){0,255}$[/font]" doesn't work either and it doesn't use "Alternation" That may be to do with the quote marks but who knows. I'll keep testing and posting what I find.
Alternation should work fine. We just pass the regex directly to preg_match(). We do use pipes to delimit the regex, but we escape all pipes within the regex content first. I suggest logging a bug report so a developer takes a look into it. The escaping may not be working.
The bug tracker is at: http://bugs.matrix.squiz.ne
[quote]Alternation should work fine. We just pass the regex directly to preg_match(). We do use pipes to delimit the regex, but we escape all pipes within the regex content first. I suggest logging a bug report so a developer takes a look into it. The escaping may not be working.
The bug tracker is at: http://bugs.matrix.squiz.ne[/quote]
OK Thanks for that.
I’ll keep testing before I lodge a bug report. See if I can identify what else (if anything) might be causing the problem.
[quote]OK Thanks for that.
I'll keep testing before I lodge a bug report. See if I can identify what else (if anything) might be causing the problem.[/quote]
After more thorough testing the issue is definitely to do with the alternation, or the pipe.
I've posted a bug report.