I would like to validate point in a two-dimensional rectangular Cartesian coordinate. I found following regular expression solution on stackoverflow ^(\-?\d+(\.\d+)?),\s*(\-?\d+(\.\d+)?)$ . Another solution is using little PHP controls:

    $xy = explode(",", "-3,3");
    $return = array(0,0);

    if(is_array($xy) && count($xy) == 2) {
        $return = $xy;
        foreach($xy as $digit) {
            if(!is_numeric($digit)) {
                $return = array(0,0);

Although, seconds solution is "larger", I prefer this for readability. What elements should I use to make the right choice between those two solutions?


1 Answer 1


Here are some points you should take in consideration:

  • Readability: long regular expressions are difficult to understand. Use comments (and eventually break one long expression into several small ones, if possible) to make it easier to read. At the same time, the piece of PHP code in your question can (and should) be refactored to make it more readable.

  • Readability by beginner programmers: if your team has beginner programmers, they may be completely lost and horrified when facing a long regular expression. In the same way, beginners may prefer imperative style over functional one.

    In a case of doubt, talk with your team. Ask if everyone is fine using regular expressions. Show this one (with comments) and ask if they understand it. If you work with experienced developers, you may also add comments, then introduce a error into the expression itself, and ask them if they notice anything wrong.

  • Performance: benchmark extensively both solutions. It might be that the one is terribly slow compared to another one. Don't guess—measure.

    Note that if both conform to your expectations, you shouldn't take the performance element into consideration. For example, if you expect the code to run in 5 ms., and one piece of code takes 14 µs, while the second one takes 960 µs, don't take the first one just because it is faster.

  • Maintenance: if you expect the code to be modified, think twice. Would it be easy to modify the regular expression (once it is properly commented)? Is it safe to modify the piece of code without breaking anything?

    Having regression testing ensures you can modify code without breaking it, but that's not the point. If the code is difficult to modify, nobody will dare touching it, even with regression tests.

Finally, you must obviously understand both to pick the right one for you/your project/your team. This means that:

  • You should be able to add relevant comments to the regular expression,

  • You should be able to see something wrong in the pice of PHP code you copy-pasted and be able to refactor this.

  • Note that often you want "validate and parse" (e.g. return integer co-ords) and not "validate, then parse" - for the latter case, half the work needs to be done twice. Also, for all parsing good code returns descriptive errors (was the comma missing? was the integer out of range? was the string empty?).
    – Brendan
    Nov 27, 2014 at 3:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.