I want to be able to validate a user's inputted regex, to check if it's valid or not. First thing I found with PHP's filter_var with the FILTER_VALIDATE_REGEXP constant but that doesn't do what I want since it must pass a regex to the options but I'm not regex'ing against anything so basically it's just checking the regex validity.
But you get the idea, how do I validate a user's inputted regex (that matches against nothing).
Example of validating, in simple words:
$user_inputted_regex = $_POST['regex']; // e.g. /([a-z]+)\..*([0-9]{2})/i
if(is_valid_regex($user_inputted_regex))
{
// The regex was valid
}
else
{
// The regex was invalid
}
Examples of validation:
/[[0-9]/i // invalid
//(.*)/ // invalid
/(.*)-(.*)-(.*)/ // valid
/([a-z]+)-([0-9_]+)/i // valid
Here's an idea (demo):
function is_valid_regex($pattern)
{
return is_int(#preg_match($pattern, ''));
}
preg_match() returns the number of times pattern matches. That will be
either 0 times (no match) or 1 time because preg_match() will stop
searching after the first match.
preg_match() returns FALSE if an error occurred.
And to get the reason why the pattern isn't valid, use preg_last_error.
You would need to write your own function to validate a regex. You can validate it so far as to say whether it contains illegal characters or bad form, but there is no way to test that it is a working expression. For that you would need to create a solution.
But then you do realize there really is no such thing as an invalid regex. A regex is performance based. It either matches or it doesn't and that is dependent upon the subject of the test--even if the expression or its results are seemingly meaningless.
In other words, you can only test a regular expression for valid syntax...and that can be nearly anything!
Related
I'm using PHP's PCRE, and there is one bit of the regex I can't seem to do. I have a character class with 5 characters [adjxz] which can appear or not, in any order, after a token (|) on the string. They all can appear, but they can only each appear once. So for example:
*|ad - is valid
*|dxa - is valid
*|da - is valid
*|a - is valid
*|aaj - is *not* valid
*|adjxz - is valid
*|addjxz - is *not* valid
Any idea how I can do it? a simple [adjxz]+, or even [adjxz]{1,5} do not work as they allow repetition. Since the order does not matter also, I can't do /a?d?j?x?z?/, so I'm at a loss.
Perhaps using a lookahead combined with a backreference like this:
\|(?![adjxz]*([adjxz])[adjxz]*\1)[adjxz]{1,5}
demonstration
If you know these characters are followed by something else, e.g. whitespace you can simplify this to:
\|(?!\S*(\S)\S*\1)[adjxz]{1,5}
I think you should break this in 2 steps:
A regex to check for unexpected characters
A simple PHP check for duplicated characters
function strIsValid($str) {
if (!preg_match('/^\*|([adjxz]+)$/', $str, $matches)) {
return false;
}
return strlen($matches[1]) === count(array_unique(str_split($matches[1])));
}
I suggest using reverse logic where you match the unwanted case using this pattern
\|.*?([adjxz])(?=.*\1)
Demo
I have been trying to validate a form where the input is the first and last name using regex in PHP. All I need the regex to do is check to make sure that there are no numbers. This is what I have right now:
if (preg_match('/\A\b[^0-9]*\W[^0-9]*\b\Z/sm', $name)) {
# Successful match
$nameError = "";
echo $name;
} else {
# Match attempt failed\
$nameError = "No Numbers";
}
The $name variable holds First and last name. I have been trying to make this work and I have not been able to get the input to match the regex. Am I using this correctly or do I need to input it in another way. Thank you for your help
if name is surename and first name you should use condition depending on country for example in Poland it would be
preg_match('/[a-z]+ [a-z]+/i',$name);
It means that all the names that contains two part that are alphabetic with space separating them are good. If you want first letter of name to be upper you should change it to
preg_match('/[A-Z][a-z]+ [A-Z][a-z]+/',$name);
Preg_match returns true if $name is validated by regular expression that you provide in the first argument.
So your usage of this function is okay, you should check your expression.
http://pl1.php.net/preg_match
preg_match() returns 1 if the pattern matches given subject, 0 if it does not, or FALSE if an error occurred.
You can always check your regex on online checker for example
http://www.solmetra.com/scripts/regex/
If you just want two words separated by one space, this will do what you want: if (preg_match('/^[A-Za-z]+ [A-Za-z]+$/', $name))
Thank you all for your replies, I found the answer in the most obvious place though and it didn't have anything to do with the regex. I forgot to setup the variables correctly for using them on the same page as the form. Stupid mistake. Anyway, thank you again.
I have made this regex:
(?<=span class="ope">)?[a-z0-9]+?\.(pl|com|net\.pl|tk|org|org\.pl|eu)|$(?=<\/span>)$
It does match the strings like: example.pl, example12.com, something.eu but it will also match the dontwantthis.com.
My question is how to don't match a string in case if it contains the dontwantthis string?
You're probably following your regex with a loop to cycle through matches. In this case, it's probably easiest to just check for the presence of the dontwantthis substring and continue if it's there. Trying to implement it in regex is just asking for trouble.
It seems that you are extracting content from span elements using a regular expression. Now, despite all the reasons why this is not such a good idea...
... just keep the expression you have. Then, if you have a match, filter out the matched entries that should be rejected.
var $match = extractContentFromHtml($html); // use regex here, return false if no match
if ($match && validMatch($match)) {
// do something
}
where validMatch(string) should check if the value exists in some array, for example.
I am trying to validate form input data using PHP's preg_match function. I am a little confused of how to use it. If I want to validate say an alphanumeric string, I would use ^[0-9a-zA-Z ]+$ as the first parameter and the string we're validating as the second one. But how would I use preg_match to tell if it's valid or not? Would I do this:
if(preg_match("^[0-9a-zA-Z ]+$", $_POST['display_name'])){
"String is valid";
} else {
"String is not valid";
}
Or the other way around? I am currently using the if not preg_match if statement but it's returning false for some reason... I know this is probably an easy answer, but I cannot figure this out.
FALSE return from a preg_match indicates an error
you need to delimit your regex (see the leading and trailing / you can use other characters too
if (preg_match("/^[0-9a-zA-Z ]+$/", $_POST['display_name'])) {
You need add the delimiters of your pattern, like this:
preg_match("/^[0-9a-zA-Z ]+$/", $_POST['display_name'])
Is there a regexp to check if a string is a valid php regexp ?
In the back office of my application, the administrator define the data type and he can define a pattern as a regexp. For example /^[A-Z][a-zA-Z]+[a-z]$/ and in the front office, i use this pattern for validate user entries.
In my code i use the PHP preg_match function
preg_match($pattern, $user_entries);
the first argument must be a valid PHP regexp, how can i be sure that $pattern is a valid regexp since it a user entrie in my back office.
Any idea ?
Execute it and catch any warnings.
$track_errors = ini_get('track_errors');
ini_set('track_errors', 'on');
$php_errormsg = '';
#preg_match($regex, 'dummy');
$error = $php_errormsg;
ini_set('track_errors', $track_errors);
if($error) {
// do something. $error contains the PHP warning thrown by the regex
}
If you just want to know if the regex fails or not you can simply use preg_match($regex, 'dummy') === false - that won't give you an error message though.
As a work-around, you could just try and use the regex and see if an error occurs:
function is_regex($pattern)
{
return #preg_match($pattern, "") !== false;
}
The function preg_match() returns false on error, and int when executing without error.
Background: I don't know if regular expressions themselves form a regular grammar, i.e. whether it's even possible in principle to verify a regex with a regex. The generic approach is to start parsing and checking if an error occurs, which is what the workaround does.
Technically, any expression can be a valid regular expression...
So, the validity of a regular expression will depend on the rules you want to respect.
I would:
Identify the rules your regex must do
Use a preg_match of your own, or some combination of substr to validate the pattern
You could use T-Regx library:
<?php
if (pattern('invalid {{')->valid()) {
https://t-regx.com/docs/is-valid