need laravel regular expression pattern for index number validation - php

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}$/',

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

Combine two regular expressions for php

I have these two regular expression
^(((98)|(\+98)|(0098)|0)(9){1}[0-9]{9})+$
^(9){1}[0-9]{9}+$
How can I combine these phrases together?
valid phone :
just start with : 0098 , +98 , 98 , 09 and 9
sample :
00989151855454
+989151855454
989151855454
09151855454
9151855454
You haven't provided what passes and what doesn't, but I think this will work if I understand correctly...
/^\+?0{0,2}98?/
Live demo
^ Matches the start of the string
\+? Matches 0 or 1 plus symbols (the backslash is to escape)
0{0,2} Matches between 0 and 2 (0, 1, and 2) of the 0 character
9 Matches a literal 9
8? Matches 0 or 1 of the literal 8 characters
Looking at your second regex, it looks like you want to make the first part ((98)|(\+98)|(0098)|0) in your first regex optional. Just make it optional by putting ? after it and it will allow the numbers allowed by second regex. Change this,
^(((98)|(\+98)|(0098)|0)(9){1}[0-9]{9})+$
to,
^(?:98|\+98|0098|0)?9[0-9]{9}$
^ this makes the non-grouping pattern optional which contains various alternations you want to allow.
I've made few more corrections in the regex. Use of {1} is redundant as that's the default behavior of a character, with or without it. and you don't need to unnecessarily group regex unless you need the groups. And I've removed the outer most parenthesis and + after it as that is not needed.
Demo
This regex
^(?:98|\+98|0098|0)?9[0-9]{9}$
matches
00989151855454
+989151855454
989151855454
09151855454
9151855454
Demo: https://regex101.com/r/VFc4pK/1/
However note that you are requiring to have a 9 as first digit after the country code or 0.

Use regex to verify an ISBN number

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

Regular expression to extract first letters of UK postcode

I want to extract the area code from a UK postcode using a regular expression. For example this would be removing "SW" from "SW11 1AW". The area is always the first characters of the string and is always followed by a number. I can't just extract the first two characters as sometimes there is only one letter, eg "E1 4PN". So it needs to match A-Z only from the start of the string until it hits a number and return just the letters. For the sake of argument the string will always be upper case.
Thanks.
(assuming PHP)
$letters = preg_replace('#^([a-z]+).*#i','$1',$postcode);
In ruby this would look like:
postcode = 'SW11 1AW'
postcode[/^[a-z]+/i] # get the area code
#=> "SW"
postcode[/^[a-z]+(.*)/i,1] # get the rest
#=> "11 1AW"
Note: The i flag (ignore case) is set. So both, uppercase and downcase letters work.
^(?i)[a-z]+(?=\d)
Will find first two letters if there is two or first one if only one letter that is before a number, regardless of case.

Search a String for Alpha Numeric Characters in a Pattern

I have a string that contains 5 words. In the string one of the words is a Ham Radio Call Sign and can be anyone of the thousands of call signs in the US. In order to extract the Call Sign from the string I need to utilize the below pattern. The Call Sign I need to extract can be in any of the 5 positions in the string. The number is never the first character and the number is never the last character. The string is actually put together from an Array since it is originally read from a text file.
$string = $word[1] $word[2] $word[3] etc....
So the search can be either done on the whole string or each piece of the array.
Patterns:
1 Number and 3 Letters Example: AB4C A4BC
1 Number and 4 Letters Example: A4BCD
1 Number and 5 Letters Example: AB4CDE
I have tried everything I can think of and search till I cant search no more. I am sure I am over thinking this.
A two-step regular expression like this would do it:
$str = "hello A4AB there BC5AD";
$signs = array();
preg_match_all('/[A-Z][A-Z\d]{1,3}[A-Z]/', $str, $possible_signs);
foreach($possible_signs[0] as $possible_sign)
if (preg_match('/^\D+\d\D+$/', $possible_sign))
array_push($signs, $possible_sign);
print_r($signs); //Array ([0] => A4AB [1] => BC5AD)
Explanation
This is a regular expression approach, using two patterns. I don't think it could be done with one and still satisfy the exact requirements of the matching rules.
The first pattern enforces the following requirements:
substring starts and ends with a capital letter
substring contains only other capital letters or numbers between the first and last letter
substring is, overall, not more than 6 characters long
What I can't do in that same pattern, for complex REGEX reasons I won't go into (unless someone knows a way and can correct me), is enforce that only one number is contained.
#jeroen's answer does enforce this in a single pattern, but in turn does not enforce the correct length of the substring. Either way, we need a second pattern.
So after grabbing the initial matches, we loop over the results. We then apply each to a second pattern that enforces simply that there is only one number in the substring.
If so, we green-light the substring and it's added to the $signs array.
Hope this helps.
It depends on what the other words can contain, but you could use a regular expression like:
#\b[a-z]+\d[a-z]+\b#i
^ case insensitive
^^ a word boundary
^^^^^^ One or more letters
^^ One number
You can make it more restrictive by using {1,3} instead of + for the letters so that you have a sequence of 1 to 3 letters.
The complete expression would be something like:
$success = preg_match('#\b[a-z]+\d[a-z]+\b#i', $input_string, $matches);
where $matches[0] will contain the matched value, see the manual.

Categories