Use regex to verify an ISBN number - php

I need a regex to verify ISBN number entered by user.
ISBN must be a string contains only:
[10 or 13 digits] and hyphens
I tried ^[\d*\-]{10}|[\d*\-]{13}$ but it doesn't work.
My regex only matches: 978-1-5661, 1-56619-90, 1257561035
It should returns the results below:
"978-1-56619-909-4 2" => false
"978-1-56619-909-4" => true
"1-56619-909-3 " => false
"1-56619-909-3" => true
"isbn446877428ydh" => false
"55 65465 4513574" => false
"1257561035" => true
"1248752418865" => true
I really appreciate any help.

You can use this regex with a positive lookahead:
^(?=(?:\D*\d){10}(?:(?:\D*\d){3})?$)[\d-]+$
RegEx Demo
(?=(?:\D*\d){10}(?:(?:\D*\d){3})?$) is a positive lookahead that ensures we have 10 or 13 digits in the input.

As mentioned at the accepted answer, not all 10 or 13 digit numbers are valid ISBN.
An ISBN consists of five groups of numbers that make out 13 digits. In 2007 the standard moved from 10 digits. The five groups can accept various lengths of numbers, which makes ISBN challenging to validate.
Ref. https://en.wikipedia.org/wiki/International_Standard_Book_Number
One solution is this:
^(?:ISBN(?:-13)?:?\ )?(?=[0-9]{13}$|(?=(?:[0-9]+[-\ ]){4})[-\ 0-9]{17}$)97[89][-\ ]?[0-9]{1,5}[-\ ]?[0-9]+[-\ ]?[0-9]+[-\ ]?[0-9]$
Source: O'Reilly Regular Expressions Cookbook, 2nd edition
You may find many possible regexp for ISBN validation here: https://regexlib.com/Search.aspx?k=ISBN

Related

Regex - multiple matches to accept

I am fairly new to Regex and trying to carry out 3 separate matches on a string, using .php. All 3 must match in order for it to be accepted.
I have a string that will go into a form that specifies the following:
KON-92382
Where KON (Konica) can either be: KON, HEW or CAN
Where '-' is mandatory in this position
Where 5 (strict) digit code must start with 9
So, I need to check the first 3 positions and then TRUE or FALSE
If TRUE, check 4th position and then TRUE or FALSE
Check 5th-9th position that they start with a 9 and then TRUE or FALSE
All 3 checks must result in TRUE, otherwise the check should return FALSE
End users will enter the printer tag into our internal ticketing system and these strings equate to a printer in our assets database.
So far I have tested the following as working, but I need to expand the code so that the '9' can be either 4, 5, 6, 7, 8 or 9 as I am planning to use this number to identify the class of printer (ie - 4 will be A3 Colour MFD)
/(KON|HEW|CAN)(-)(9)[0-9][0-9][0-9][0-9]/
My 'dirty' fix is as follows:
$format = "/(KON-4)|(KON-9)|(HEW-4)|(HEW-9)|(CAN-4)|(CAN-9)(-)[0-9][0-9][0-9][0-9]/";
In this example, i've only specified type 4 or 9 to keep it simple, because the question is more about matching ALL 3 criteria.
Another note, I do not want the 4-9 to be a range (reason being that I want type 5 to fail (ie. KON-51234) as I will be taking all type 5 B/W MFDs out of service pending provider removing from sites and therefore any ticket with this string should fail so that service desk know that this is not an active printer)
Any help is appreciated
You could specify different allowed numbers for every match:
^(?:KON-[46-9]|HEW-[4-9]|CAN-[4-9])\d{4}$
Explanation
^ Start of string
(?: Non capture group for the alternatives
KON-[46-9]|HEW-[4-9]|CAN-[4-9]
) Close non capture group
\d{4} Match 4 digits
$ End of string
See a regex101 demo.
Example for valid matches:
$strings = [
"KON-92382",
"KON-51234",
"HEW-71111",
"CAN-31111",
"KON-912345"
];
$pattern = "/^(?:KON-[46-9]|HEW-[4-9]|CAN-[4-9])\d{4}$/";
print_r(preg_grep($pattern, $strings));
Output
Array
(
[0] => KON-92382
[2] => HEW-71111
)

need laravel regular expression pattern for index number validation

validating index_number with regular expression
i want the index number to start with 'UST' followed by 9 digits
example UST16280180
'index_number' => 'required|unique:students|min:11|max:11|regex:(UST)+{0-9}/i',
this is the results that i want : UST16280149
You can try this regex rule, case sensitive on UST:
'index_number' => 'regex:/^UST\d{9}$/',
This is for case insensitivity (it accepts UST, ust, Ust etc.):
'index_number' => 'regex:/^UST\d{9}$/i',
Change the {9} in the regex with the number of digits you want after UST, example for 8 digits:
'index_number' => 'regex:/^UST\d{8}$/',

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

Regular Expression in PHP to extract all possible combination of phone/mobile numbers using one Regex

i am using php and wants to extract phone/mobile numbers from string, i have string with multiple format of phone numbers like
$str = '(123) 456-7890 or (123)456-7890 and 1234567890 test "123.456.7890" another test "123 456 7890"';
i had write one RE as,
$phoneMatches = '';
$str = '(123) 456-7890 or (123)456-7890 or 1234567890 or "123.456.7890" or "123 456 7890"';
$phonePattern = '/\b[0-9]{3}\s*[-]?\s*[0-9]{3}\s*[-]?\s*[0-9]{4}\b/';
preg_match_all($phonePattern, $str, $phoneMatches);
echo "<pre>";
print_r($phoneMatches);
exit;
but it gives me output like this,
Array
(
[0] => Array
(
[0] => 1234567890
[1] => 123 456 7890
)
)
Means only two, but i want all the possible combination of phone numbers and mobile numbers from string of text by using only ONE Regular expression.
Thanks
I know I'm late, and I'm not sure if this is what you wanted, but I came up with this solution:
[+()\d].*?\d{4}(?!\d)
Demonstration: regex101.com
Explanation:
[+()\d] - We start by matching anything that might represent the start of a phone number.
.*?\d{4} - Then we match anything (using a lazy quantifier) until we reach four ending digits. Just a little note: I considered this as a rule, but it might not always apply. You'd then need to modify the regex to include other cases.
(?!\d) - This is a negative lookahead and it means that we don't want any matches followed by a digit character. I used this to avoid some half-matches.
Another observation is that this regex doesn't validate any phone number. You could have anything in between the matches, mainly because of this part: .*?\d{4}. This will work depending on what kind of situation you intend to use it.

PHP Regex - accept all positive numbers except for 1

Regexes and I have a relationship of love and hate. I need to match (accept) all numbers except for number 1 and 0. Seeing it as math and not string, number >= 2 should be matched. Also please consider this is part of a Zend route param (reqs) so I have to go with regex unless I want to extend Route class, and etc. etc. :)
103 => 103
013 => 013
201 => 201
340 => 340
111 => 111
001 => no match
010 => 010
100 => 100
1 => no match
000 => no match
00 => no match
0 => no match
I've tried some variations of [^1][|\d+] (trying to nail one digit at a time :D) but so far I've failed horribly :(
Nailed it!!
The regex I was looking for appears to be the following
^([2-9]|[2-9]\d|[1-9]\d{1,})$
Just use negative lookahead to exclude patterns with all zeroes that optionally end in an one:
/^(?!0*1?$)\d+$/
If you read it without the parens, this regex matches anything that consists of one or more decimal digits. The parens contain an assertion that causes the regex to match only if the pattern 0*1?$ cannot be matched beginning at the start of the input, so this removes the scenario of all zeroes and one with any number of prepended zeroes.
Use negation on the result for matching all zeroes and ones
if(!preg_match("^[01]+$",$string)) {...}
Your thinking of it the wrong way.
^[01]+$
will match all that onyl contain 0 or 1.
If that matches reject it, if it doesnt match check its a valid number and you should have a match
//if the numbers only include 0 or 1 reurn false;else will return the numbers
function match_number($number){
if(preg_match("/^[01]*$/", $number) > 0){
return false;
} else {
return $number;
}
}
The following regex will match any number (not digits, but number as a whole) >= 2
^([2-9]|[2-9]\d|[1-9]\d{1,})$
Thanks for all the valuable help in the answers, some were really helpful and helped me reach the desired result.

Categories