Comparison of a string with given pattern - php

I am taking an input from the user in form field. I wanna make sure that the given input should be in the pattern like "2012-xxx-111" i-e 1st four should be integers and there should be a "-" sign after that there should be two or three alphabets after that the "-" and at the end any integer value consisting of 3 numbers. Help me doing all this in php. Thanks
your help would be appreciated .

You can use a regex for that.
[0-9]{4}-[A-Za-z]{2,3}-[0-9]{3}
This will match any number between 0 and 9 four times, following by a -, followed by 2 or 3 letters from a to z, lowercase or uppercase, and finally another - and 3 more numbers.
http://regexr.com/3bo81
In PHP, you can use preg_match() to see if a string matches a given pattern.

Use preg_match with a regex
if (preg_match("/^[0-9]{4}-[A-Za-z]{2,3}-[0-9]{3}$/", "Search String Here")) {
echo "A match was found.";
} else {
echo "A match was not found.";
}

Related

Need help for a regular expressions with PHP Preg_match to validates a number field on our form

:)
We would like to set a special condition (based on PHP Preg_match regular expression) to validates a number on our form.
That “number field” need, at first, only contain a max of 13 numbers (and only numbers. No letters or anything else).
The very first number need to be (only) “1” or “2” (not anything else)
The 4rd and 5rd number represent (the 2 numbers combinated) the “Month of birth” of someone, so the 4rd number need to be "0" or "1", and the 5rd need to be between "1" and "9".
Really appreciates if you can help us for that, to have the good “syntax” for the regular expression in PHP Preg_match to validates that field on our form! :)
Thanks to the community for your support and help!
Regards
Here is the literal regex pattern you have described to us:
^[12]\d{2}(?:0[1-9]|1[0-2])\d{8}$
Sample script:
$input = "1231212345678";
if (preg_match("/^[12]\d{2}(?:0[1-9]|1[0-2])\d{8}$/", $input)) {
echo "MATCH";
}
This regex pattern says to:
^ from the start of the string
[12] match 1 or 2 as the first digit
\d{2} then match any digits in the 2nd and 3rd position
(?:0[1-9]|1[0-2]) match 01, 02, ..., 12 as the two digit month
\d{8} then match any other 8 digits
$ end of string

PHP Regex Regular Expressions preg_match() only allow digits with commas

I've asked and it was answered but now, after years, it doesn't work.
I've even tried online regex validators. Not sure what is going on.
Version: PHP 7.0.30 on 64Bit OS
The string should only allow digits with commas.
No commas in the beginning or end.
Spaces between commas is ok but I'd rather not allow it.
The following isn't passing
My regex is:
$DateInvoicedIDs = "1031,453,808,387,111,342,962,706,251,442,362,858,950,738,310,288,99,665,1023,30,894,112,132,148,347,895,382,94,766,683,276,1104,658,34,348,235,786,769,2";
$reg = '/[0-9\s]+(,[0-9\s]+)*[0-9]$/';
if ( preg_match($reg, $DateInvoicedIDs) ) {
echo = $DateInvoicedIDs;
} else { echo "false"; }
I'm using preg_match and getting false.
Any idea?
Test your string and pattern # https://regex101.com/r/3TVmOv/1
When that loads, you will see that there is no match highlighted.
Then add a digit to the end of your string and Whalla! This is because (,[0-9\s]+)* is matching the final 2 and [0-9]$ cannot be satisfied because another digit is required.
If I understand your logic/requirements, I think I'd use ~^\d+(?:\s*,\s*\d+)*$~
This improves the validation because it doesn't allow a mixture of digits and spaces between commas like: 2, 3 4 56, 72 I don't think you want spaces in your comma-separated numerical values.
Pattern Demo
Code: (Demo)
$DateInvoicedIDs = "1031,453,808,387,111,342,962,706,251,442,362,858,950,738,310,288,99,665,1023,30,894,112,132,148,347,895,382,94,766,683,276,1104,658,34,348,235,786,769,2";
$reg = '/^\d+(?:\s*,\s*\d+)*$/';
if (preg_match($reg, $DateInvoicedIDs)) {
echo $DateInvoicedIDs;
} else {
echo "false";
}
It is not matching because of the last [0-9] in your regex. The * in (,[0-9\s]+)* is a greedy match which means that it is consuming all commas followed by digits in your string. There is nothing left after to match against the last [0-9].
So you probably want to reduce your regex to '/[0-9\s]+(,[0-9\s]+)*$/.
The last part of your regex [0-9]$ is what's causing it to fail:
[0-9\s]+ is matching the first number only 1031,
(,[0-9\s]+)* is covering everything until ,2 because it's a single number right after a comma which is what it's looking for
Then [0-9]$ is trying to find one more number but it can't
If the last number is a double-digit number, i.e. ,25 instead of 2, then the that second part (,[0-9\s]+)* would be satisfied because it found at least one number and [0-9]$ would match the next number which is 5 (https://regex101.com/r/0XbHsw/1)
Adding ? for that last part would solve the problem: [0-9\s]+(,[0-9\s]+)*[0-9]?$

Using regex to extract first half of string

I have variable strings like the below:
The.Test.String.A01Y18.123h.WIB-DI.DO5.1.K.314-ECO
The.Regex.F05P78.123h.WIB-DI.DO5.1.K.314-EYT
Word.C05F78.342T.DSW-RF.EF5.2.F.342-DDF
I would like to extract this part of these string in PHP dynamically and i was looking at using regex but haven't had much success:
The.Test.String.A01Y18
The.Regex.F05P78
Word.C05F78
And ultimately to:
The Test String A01Y18
The Regex F05P78
Word C05F78
The first part of the text will be variable in length and will separate each word with a period. The next part will always be the same length with the pattern:
One letter, 2 number, one letter, 2 numbers pattern (C05F78)
Any thing in the string after that is what I would like to remove.
that's it
$x=array(
"The.Test.String.A01Y18.123h.WIB-DI.DO5.1.K.314-ECO",
"The.Regex.F05P78.123h.WIB-DI.DO5.1.K.314-EYT",
"Word.C05F78.342T.DSW-RF.EF5.2.F.342-DDF"
);
for ($i=0, $tmp_count=count($x); $i<$tmp_count; ++$i) {
echo str_replace(".", " ", preg_replace("/^(.+?)([a-z]{1}[0-9]{2}[a-z]{1}[0-9]{2})\..+$/i", "\\1\\2", $x[$i]))."<br />";
}
Using this regular expression should work, replacing each of your strings with the first capturing group:
^((?:\w+\.)+\w\d{2}\w\d{2}).*
See demo at http://regex101.com/r/fR3pM6
This is valid too:
preg_match("\.*[\w\d]{6}", stringVariable)
.* for all digits atleast we found a composition of letters and words of 6 characters ([\w\d]{6})
Result:
Match 1: The.Test.Stsrisng.A01Y18
Match 2: The.Regex.F05P78
Match 3: Word.C05F78

PHP / regular expression to check if a string contains a word of a certain length

I need to check whether a received string contains any words that are more than 20 characters in length. For example the input string :
hi there asssssssssssssssssssskkkkkkkk how are you doing ?
would return true.
could somebody please help me out with a regexp to check for this. i'm using php.
thanks in advance.
/\w{20}/
...filller for 15 characters....
You can test if the string contains a match of the following pattern:
[A-Za-z]{20}
The construct [A-Za-z] creates a character class that matches ASCII uppercase and lowercase letters. The {20} is a finite repetition syntax. It's enough to check if there's a match that contains 20 letters, because if there's a word that contains more, it contains at least 20.
References
regular-expressions.info/Character Classes and Finite Repetition
PHP snippet
Here's an example usage:
$strings = array(
"hey what the (##$&*!#^#*&^#!#*^##*##*&^#!*#!",
"now this one is just waaaaaaaaaaaaaaaaaaay too long",
"12345678901234567890123 that's not a word, is it???",
"LOLOLOLOLOLOLOLOLOLOLOL that's just unacceptable!",
"one-two-three-four-five-six-seven-eight-nine-ten",
"goaaaa...............aaaaaaaaaalll!!!!!!!!!!!!!!",
"there is absolutely nothing here"
);
foreach ($strings as $str) {
echo $str."\n".preg_match('/[a-zA-Z]{20}/', $str)."\n";
}
This prints (as seen on ideone.com):
hey what the (##$&*!#^#*&^#!#*^##*##*&^#!*#!
0
now this one is just waaaaaaaaaaaaaaaaaaay too long
1
12345678901234567890123 that's not a word, is it???
0
LOLOLOLOLOLOLOLOLOLOLOL that's just unacceptable!
1
one-two-three-four-five-six-seven-eight-nine-ten
0
goaaaa...............aaaaaaaaaalll!!!!!!!!!!!!!!
0
there is absolutely nothing here
0
As specified in the pattern, preg_match is true when there's a "word" (as defined by a sequence of letters) that is at least 20 characters long.
If this definition of a "word" is not adequate, then simply change the pattern to, e.g. \S{20}. That is, any seqeuence of 20 non-whitespace characters; now all but the last string is a match (as seen on ideone.com).
I think the strlen function is what you looking for. you can do something like this:
if (strlen($input) > 20) {
echo "input is more than 20 characters";
}

Bug in a PHP regex

I am trying to print "1" if there are at least two of the same figure in the match, else 0.
What is wrong in the regex?
if ( max ( array_map ('strlen', preg_split('/([0-9])[^0-9]*\1/', "1 2 3 1 4") ) ) == 1 )
echo 1;
else
echo 0;
echo preg_match('/(?<=^|[^0-9])([0-9)+)(?=[^0-9]).*(?<=[^0-9])\1(?=[^0-9]|$)/', "1 2 3 1 4");
Will match for any repeated number in the sequence, and echo 1 if there is something repeated, 0 if not.
(Original version just looked for something repeated after each other, this matches repeated anywhere in the string)
The [^0-9]* matches any number of NON-digit characters. So if there's another number, it will fail the match. Try replacing [^0-9]* with a simple .*, which will match digits or non-digits.
Try the following code. It should print 1 when there is a repeat number.
if(0 == strlen(preg_replace('/.*([0-9]).+\1.*/', '', '1 2 3 1 5 4')))
echo 1;
else echo 0;
The regex /.*([0-9]).+\1.*/ will match a number and another number (with .+ or anything in between them).
Hope this helps.
Try a lookahead assertion. I use "The Regex Coach" whenever I'm trying to figure something out. It doesn't give you hints or anything, but it does give immediate feedback.
Test string: "1 2 3 1 4 3"
Regex: ([0-9])(?=.*\1)
Basically the ()'s around the [0-9] store the result, and the lookahead (?= matches .* - any character and then \1 - what was matched first (so it looks for any number and then looks ahead to see if that number occurs again)
This will match both "1" and "3"
I'm not quite sure if php supports lookaheads, but that's my take.

Categories