Regular expression to validate employee id - php

I want a regular expression to validate employee ids which are
UC-00001,
UC-00012,
UC-000100. etc
"UC-000" is constant but after that if one digit number exist it becomes UC-00001 and if more den one digit exist then only three zeros needs to be constant.(UC-00010)
I tried using preg_match(/[U]{1}[C]{1}-[0]{3}[0-9]$/) but its not validating properly.
Thanks in advance

You can match three zeroes and then either from 00 to 09 or from 10 up..
^UC-000(?:0\d|[1-9]\d*)$
The pattern matches:
^ Start of string
UC-000 Match literally
(?: Non capture group
0\d Match 0 and a single digit 0-9 (Or use [1-9] to not match 00000)
| Or
[1-9]\d* Match a digit 1-9 and optional digits
) Close non capture group
$ End of string
Regex demo
$strings = [
"UC-00001",
"UC-00012",
"UC-000100",
"UC-00001",
"UC-00010",
"UC-00000",
"UC-000010",
"UC-0000100"
];
$pattern = "~^UC-000(?:0\d|[1-9]\d*)$~";
foreach ($strings as $s) {
if (preg_match($pattern, $s)) {
echo "Match: $s" . PHP_EOL;
} else {
echo "Not match: $s" . PHP_EOL;
}
}
Output
Match: UC-00001
Match: UC-00012
Match: UC-000100
Match: UC-00001
Match: UC-00010
Match: UC-00000
Not match: UC-000010
Not match: UC-0000100

Your pattern is logically correct, save that you didn't allow for multiple digits after the leading 000. I would use this version:
^UC-[0-9]{5,}$
PHP script:
$input = "UC-000100";
if (preg_match("/^UC-[0-9]{5,}$/", $input)) {
echo "VALID";
}

Related

Target message to match with regex

I have to check csv files live and match some expression to get data.
These files can have different type of message so different matching expression.
The message can be something like that
GuiPrinter.ProcessPrint of 116806 25374 K356 S Black Face.png 229 at 1
table
And I want to get 116806 25374 K356 S Black Face.png
. So the regex associate to this kind of file would be something like (GuiPrinter.ProcessPrint of )(.*)([.][png|jpg|jpeg|PNG|JPG|JPEG]*) and I can return $result[2]
But the message and the regex can change, so I need a common function that can return the string that I want based on the regex, the function would have message and regex parameters. Maybe for another file the string that I want would be on first position so my $result[2] won't work.
How can I ensure to always return the string that I want to match ?
Use
\preg_match('/GuiPrinter.ProcessPrint of(.*?)\.(gif|png|bmp|jpe?g)/', $str, $match);
print_r($match[1]);
You could match the text GuiPrinter.ProcessPrint and then use \K to reset the starting point of the reported match.
Match any character zero or more times non greedy .*?, then match a dot \. and any of the image extensions in a non capturing group (?:gif|png|bmp|jpe?g) followed by a word boundary \b
GuiPrinter\.ProcessPrint of \K.*?\.(?:gif|png|bmp|jpe?g)\b
Note that to match the dot literally you have to escape it \.
For example to return 1 match using preg_match:
$str = 'GuiPrinter.ProcessPrint of 116806 25374 K356 S Black Face.png 229 at 1 table';
$re = '/GuiPrinter\.ProcessPrint of \K.*?\.(?:gif|png|bmp|jpe?g)\b/';
function findMatch($message, $regex) {
preg_match($regex, $message, $matches);
return array_shift($matches);
}
$result = findMatch($str, $re);
if ($result) {
echo "Found: $result";
} else {
echo "No match.";
}
Demo

Regular expression doesn't work

I need to match following pattern using php Regular expression but it doesn't give expected out come.
ex:-
need to match pattern
5555 5545 9930
$id = "4567 3423 4567";
$regex = "/^[1-9]\d{4} \d{4} \d{4}$/";
if (preg_match($regex, $id)) {
// Indeed, the expression "^[2-9]\d{2} \d{3} \d{4}$" matches the date string
echo "Found a match!";
}else {
// If preg_match() returns false, then the regex does not
// match the string
echo "The regex pattern does not match. :(";
}
If you want to match: 4 non-zero digits + space + 4 digits + space + 4 digits
^([1-9]){4} \d{4} \d{4}$ should do the trick
I think the safest way to modify the existing regex is by adding an alternative to the first [2-9]\d{2} \d{3}:
^(?:[2-9]\d{2} \d{3}|\d{4} \d{4}) \d{4}$
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
See the regex demo
Details:
^ - start of string
(?:[2-9]\d{2} \d{3}|\d{4} \d{4}) - one of the alternatives:
[2-9]\d{2} \d{3} - a digit from 2 to 9, any 2 digits, a space and 3 digits
| - or
\d{4} \d{4} - 4 digits, space, 4 digits (for the new string types)
- a space
\d{4} - 4 digits
$ - end of string.
See the PHP demo:
$id = "4567 3423 4567";
$regex = "/^(?:[2-9]\d{2} \d{3}|\d{4} \d{4}) \d{4}$/";
if (preg_match($regex, $id)) {
echo "Found a match!";
} else {
echo "The regex pattern does not match. :(";
}
Simple edit of your regular expression will be
^[1-9]\d{3} \d{4} \d{4}

PHP: Regular exp to get the last number from exactly string

I'm trying to get the string that match with original and with number in the end.
I got these strings:
mod_courts2
mod_courts_config
mod_courts_config2
From these strings I want the one that matches only with "mod_courts" with number in the end.
I'm doing this:
if (strpos($t, "mod_courts") !== FALSE) {
preg_match('/^\w+(\d+)$/U', $t, $match);
echo $match;
}
This returns me "mod_courts2" and "mod_courts_config2", I just want "mod_courts2"
Use the following regex:
/^[a-z]+_[a-z]+(\d+)$/
Explanation:
^ - assert position at the beginning of the string
[a-z]+ - match any alphabet one or more times
_ - match a literal undescore character
[a-z]+ - match any alphabet one or more times
(\d+) - match (and capture) any digit from 0 to 9 one or more times
$ - assert position at the end of the string
Test cases:
$array = array(
'mod_courts2',
'mod_courts_config',
'mod_courts_config2'
);
foreach ($array as $string) {
if(preg_match('/^[a-z]+_[a-z]+(\d+)$/i', $string, $matches)) {
print_r($matches);
}
}
Output:
Array
(
[0] => mod_courts2
[1] => 2
)
Very simply, you can do:
/^(mod_courts\d+)$/
However, if you want exactly the following format: sometext_somettext2, you can use the following regex:
/^([a-zA-Z]+_[a-zA-Z]+\d+)$/
or
/^([^_]+_[^_]+\d+)$/
Demos
http://regex101.com/r/jP8iC1
http://regex101.com/r/tI1uX8
http://regex101.com/r/fX8pO5
^mod_courts\d+$
this should do it
You can just use
^mod_courts[0-9]+$
Meaning mod_courts followed by a number (and only that, thanks to ^$ matching the beginning and end of the string). No need for the strpos check.

php regular expression minimum and maximum length doesn't work as expected

I want to create a regular expression in PHP, which will allow to user to enter a phone number in either of the formats below.
345-234 898
345 234-898
235-123-456
548 812 346
The minimum length of number should be 7 and maximum length should be 12.
The problem is that, the regular expression doesn't care about the minimum and maximum length. I don't know what is the problem in it. Please help me to solve it. Here is the regular expression.
if (preg_match("/^([0-9]+((\s?|-?)[0-9]+)*){7,12}$/", $string)) {
echo "ok";
} else {
echo "not ok";
}
Thanks for reading my question. I will wait for responses.
You should use the start (^) and the end ($) sign on your pattern
$subject = "123456789";
$pattern = '/^[0-9]{7,9}$/i';
if(preg_match($pattern, $subject)){
echo 'matched';
}else{
echo 'not matched';
}
You can use preg_replace to strip out non-digit symbols and check length of resulting string.
$onlyDigits = preg_replace('/\\D/', '', $string);
$length = strlen($onlyDigits);
if ($length < 7 OR $length > 12)
echo "not ok";
else
echo "ok";
Simply do this:
if (preg_match("/^\d{3}[ -]\d{3}[ -]\d{3}$/", $string)) {
Here \d means any digits from 0-9. Also [ -] means either a space or a hyphen
You can check the length with a lookahead assertion (?=...) at the begining of the pattern:
/^(?=.{7,12}$)[0-9]+(?:[\s-]?[0-9]+)*$/
Breaking down your original regex, it can read like the following:
^ # start of input
(
[0-9]+ # any number, 1 or more times
(
(\s?|-?) # a space, or a dash.. maybe
[0-9]+ # any number, 1 or more times
)* # repeat group 0 or more times
)
{7,12} # repeat full group 7 to 12 times
$ # end of input
So, basically, you're allowing "any number, 1 or more times" followed by a group of "any number 1 or more times, 0 or more times" repeat "7 to 12 times" - which kind of kills your length check.
You could take a more restricted approach and write out each individual number block:
(
\d{3} # any 3 numbers
(?:[ ]+|-)? # any (optional) spaces or a hyphen
\d{3} # any 3 numbers
(?:[ ]+|-)? # any (optional) spaces or a hyphen
\d{3} # any 3 numbers
)
Simplified:
if (preg_match('/^(\d{3}(?:[ ]+|-)?\d{3}(?:[ ]+|-)?\d{3})$/', $string)) {
If you want to restrict the separators to be only a single space or a hyphen, you can update the regex to use [ -] instead of (?:[ ]+|-); if you want this to be "optional" (i.e. there can be no separator between number groups), add in a ? to the end of each.
if (preg_match('/^(\d{3}[ -]\d{3}[ -]\d{3})$/', $string)) {
may it help you out.
Validator::extend('price', function ($attribute, $value, $args) {
return preg_match('/^\d{0,8}(\.\d{1,2})?$/', $value);
});

Avoid regex match if pattern has string before it

I have these strings, and I want to match b=(\d+) only not ab=(\d+). How do I do it?
"ab=10&b=20" -> 20
"b=20&ab=10" -> 20
"b=20" -> 20
"ab=10" -> no match
You could use \b, like:
\bb=(\d+)
Which matches only at a word boundary (between a \w and not a \w).
Use a negative lookbehind:
/(?<!a)b=(\d+)/
Now this will match any number followed by b= if not preceded by the character a.
Test case:
$array = array(
"ab=10&b=20",
"b=20&ab=10",
"b=20",
"ab=10"
);
foreach ($array as $str) {
if (preg_match('/(?<!a)b=(\d+)/', $str, $matches)) {
echo $matches[1], PHP_EOL;
} else {
echo "No match", PHP_EOL;
}
}
Output:
20
20
20
No match
Demo
This is what I got:
(?:[^a-zA-Z])(?:b=(\d+))
(?:[^a-zA-Z]) It can not start with a-Z. You might want to change this, but you get the idea
(?:b=(\d+)) I wrapped it in a group to make the regex combine it, ?: makes sure \\1 will still be 20

Categories