I have the next code and I'm trying to know if the string its valid based on the regular expression.
I'm trying to validate only strings that follow the next sequence.
lettersOrNumbersAndunderDashes=lettersOrNumbersAndUnderdashes
But that sequence can be repeated if there is a vertical bar.
For example parameter1=value1|parameter2=value2|parameterN=valueN
if (preg_match("/((^[A-Za-z0-9_]+=[A-Za-z0-9_]+)\|?)/m", "perPd_asd=as_3_4d|asdas=asdasd"))
return 'Valid';
return 'Invalid';
I think I'm missing something or building a wrong regular expression.
The wrong thing you did is putting a ^ at the beginning of the pattern, which means that it will only match if the text is at the beginning of the string. This should solve :
if (preg_match("/(([A-Za-z0-9_]+=[A-Za-z0-9_]+)\|?)/m", "perPd_asd=as_3_4d|asdas=asdasd"))
return 'Valid';
return 'Invalid';
It is possible that parameter name starts from number?
You need more test cases for your regular expression, for example:
0=somevalue
param=value|
one_more_param=##$%^|some_param=some-value
_=VALUE|abc=***
a=1|b=2|c=3
param=0|param=1
my solution is:
^(([_A-Za-z][A-Za-z0-9_]*=[^\|=]+)\|)*([_A-Za-z][A-Za-z0-9_]*=[^\|=]+)$
Related
I dont have much knowledge on Regular expression. Help me out if i could achieve the below,
Username must need to validate this.
Only contains alphanumeric characters, underscore and dot
Underscore and dot can't be at the end or start of a username
Underscore and dot can't be next to each other
Underscore or dot can't be used multiple times in a row
I have come up with this regular expression but i cant fulfill all the above.
/(?<![a-z_|.])([a-z](?:[a-z]|_|.(?!_.))+[a-z]|[a-z]{2})(?![a-z_|.])/
Thanks in advance.
You don't need any lookbehind. Simplify your regex to this:
/^[a-zA-Z0-9](?!(?:.*\.){2}|(?:.*_){2}|.*[._]{2})[a-zA-Z0-9_.]*[a-zA-Z0-9]$/
RegEx Demo
One way to rome... (simple and easy version)
if(substr_count($string,'_')<=1 //for rule 4.
&& substr_count($string,'.')<=1 //for rule 4.
&& substr_count($string,'._')===0 //for rule 3.
&& substr_count($string,'_.')===0 //for rule 3.
&& preg_match('/^[^_\.][a-zA-Z]+[^_\.]$/',$string)// for rule 1. & 2.
) {
echo 'Valid';
}
I try to make system that can detect date in some string, here is the code :
$string = "02/04/16 10:08:42";
$pattern = "/\<(0?[1-9]|[12][0-9]|3[01])\/\.- \/\.- \d{2}\>/";
$found = preg_match($pattern, $string);
if ($found) {
echo ('The pattern matches the string');
} else {
echo ('No match');
}
The result i found is "No Match", i don't think that i used correct regex for the pattern. Can somebody tell me what i must to do to fix this code
First of all, remove all gibberish from the pattern. This is the part you'll need to work on:
(/0?[1-9]|[12][0-9]|3[01]/)
(As you said, you need the date only, not the datetime).
The main problem with the pattern, that you are using the logical OR operators (|) at the delimiters. If the delimiters are slashes, then you need to replace the tube characters with escaped slashes (/). Note that you need to escape them, because the parser will not take them as control characters. Like this: \/.
Now, you need to solve some logical tasks here, to match the numbers correctly and you're good to go.
(I'm not gonna solve the homework for you :) )
These articles will help you to solve the problem tough:
Character classes
Repetition opetors
Special characters
Pipe character (alternation operator)
Good luck!
In your comment you say you are looking for yyyy, but the example says yy.
I made a code for yy because that is what you gave us, you can easily change the 2 to a 4 and it's for yyyy.
preg_match("/((0|1|2|3)[0-9])\/\d{2}\/\d{2}/", $string, $output_array);
Echo $output_array[1]; // date
Edit:
If you use this pattern it will match the time too, thus make it harder to match wrong.
((0|1|2|3)[0-9])/\d{2}/\d{2}\s+\d{2}:\d{2}:\d{2}
http://www.phpliveregex.com/p/fjP
Edit2:
Also, you can skip one line of code.
You first preg_match to $found and then do an if $found.
This works too:
If(preg_match($pattern, $string, $found))}{
Echo $found[1];
}Else{
Echo "nothing found";
}
With pattern and string as refered to above.
As you can see the found variable is in the preg_match as the output, thus if there is a match the if will be true.
I'm having a some trouble formatting my regular expression for my PHP code using preg_match().
I have a simple string usually looking like this:
"q?=%23asdf".
I want my regular expression to only pass true if the string begins with "q?=%23" and there is a character at the end of the 3. So far one of the problems I have had is that the ? is being pulled up by the regex so doing something like
^q?=23 doesn't work. I am also having problems with contiguous searching in Regex expressions (because I can't figure out how to search after the 3).
So for clarification: "q?=%23asd" should PASS and "q?=%23" should FAIL
I'm no good with Regex so sorry if this seems like a beginner question and thanks in advance.
Just use a lookahead to check whether the character following 3 is an alphabet or not,
^q\?=%23(?=[a-zA-Z])
Add . instead of [A-Za-z] only if you want to check for any character following 3,
^q\?=%23(?=.)
Code would be,
$theregex = '~^q\?=%23(?=[a-z])~i';
if (preg_match($theregex, $yourstring)) {
// Yes! It matches!
}
else { // nah, no luck...
}
So the requirement is: Start with q?=%23, followed by at least one [a-z], the pattern could look like:
$pattern = '/^q\?=%23[a-z]+/i';
Used i (PCRE_CASELESS) modifier. Also see example at regex101.
$string = "q?=%23asdf";
var_dump(figureOut($string));
function figureOut($string){
if(strpos($string, 'q?=%23') == 0){
if(strlen($string) > 6){
return true;
}else{ return false;}
}
}
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 following string.
?page=1&sort=desc¶m=5&parm2=25
I need to check whether the enter string url is in strict format except variable value.
Like page=, sort=, param=, param2.
Please suggest regular expression.
Thanks
You should use parse_str and check if all the parameters you wanted are set with isset. Regex is not the way to go here.
Maybe this :
\?page=\d+&sort=.+¶m=\d+¶m2=\d+
which translates to :
?page= followed by any digit repeated 1 or more times
&sort= followed by any character repeated 1 or more times
¶m= followed by any digit repeated 1 or more times
¶m2= followed by any digit repeated 1 or more times
I think Alin Purcaru 's suggestion is better
EDIT:
(\?|&)(page=[^&]+|sort=[^&]+|param=[^&]+|parm2=[^&]+)
This way the order doesn't matter
If you care about the order of parameters, something like this:
\?page=[^&]+&sort=[^&]+param=[^&]+param2=[^&]+$
But Alin Purcaru is right - use the parse_str function already written to do this
The regex would be ^\?([\w\d]+=[\w\d]+(|&))*$ As long as your values are Alpha Numeric, but maybe you wanna take a look in to filters if you want to validate an url http://www.php.net/manual/en/book.filter.php
You could use the following regx /\?page=[^&]*sort=[^&]*param=[^&]*param2=/` to match:
if (preg_match("/\?page=([^&]*)sort=([^&]*)param=([^&]*)param2=([^&]*)/i", $inputstr, $matches))
{
echo "Matches:";
print_r($matches); // matches will contain the params
}
else
echo "Params nor found, or in wrong order;