Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I validate regex?

Tags:

regex

php

I'd like to test the validity of a regular expression in PHP, preferably before it's used. Is the only way to do this actually trying a preg_match() and seeing if it returns FALSE?

Is there a simpler/proper way to test for a valid regular expression?

like image 642
Ross McFarlane Avatar asked Dec 14 '10 15:12

Ross McFarlane


People also ask

How do you validate a regex pattern?

To validate a RegExp just run it against null (no need to know the data you want to test against upfront). If it returns explicit false ( === false ), it's broken. Otherwise it's valid though it need not match anything. So there's no need to write your own RegExp validator.

What is regex validator?

The RegularExpressionValidator control checks whether the value of an input control matches a pattern defined by a regular expression. This type of validation allows you to check for predictable sequences of characters, such as those in email addresses, telephone numbers, and postal codes.

What are the regular expressions and how they help in validating the inputs?

Regular Expressions are specially formatted strings for specifying patterns in text. They can be useful during information validation, to ensure that data is in a specified format. For example, a ZIP code must consist of five digits, and the last name must start with a capital letter.


2 Answers

// This is valid, both opening ( and closing ) var_dump(preg_match('~Valid(Regular)Expression~', '') === false); // This is invalid, no opening ( for the closing ) var_dump(preg_match('~InvalidRegular)Expression~', '') === false); 

As the user pozs said, also consider putting @ in front of preg_match() (@preg_match()) in a testing environment to prevent warnings or notices.

To validate a RegExp just run it against null (no need to know the data you want to test against upfront). If it returns explicit false (=== false), it's broken. Otherwise it's valid though it need not match anything.

So there's no need to write your own RegExp validator. It's wasted time...

like image 164
CodeAngry Avatar answered Sep 30 '22 12:09

CodeAngry


I created a simple function that can be called to checking preg

function is_preg_error() {     $errors = array(         PREG_NO_ERROR               => 'Code 0 : No errors',         PREG_INTERNAL_ERROR         => 'Code 1 : There was an internal PCRE error',         PREG_BACKTRACK_LIMIT_ERROR  => 'Code 2 : Backtrack limit was exhausted',         PREG_RECURSION_LIMIT_ERROR  => 'Code 3 : Recursion limit was exhausted',         PREG_BAD_UTF8_ERROR         => 'Code 4 : The offset didn\'t correspond to the begin of a valid UTF-8 code point',         PREG_BAD_UTF8_OFFSET_ERROR  => 'Code 5 : Malformed UTF-8 data',     );      return $errors[preg_last_error()]; } 

You can call this function using the follow code :

preg_match('/(?:\D+|<\d+>)*[!?]/', 'foobar foobar foobar'); echo is_preg_error(); 

Alternative - Regular Expression Online Tester

  • RegExr
  • PHP Regular Expression Tester
  • Regular Expression Tool
like image 44
Wahyu Kristianto Avatar answered Sep 30 '22 10:09

Wahyu Kristianto