i want check input by regex | contain 16 number - php

I have input, I want to check it.
It should accept only numbers, and must be 16, no more and no less.
How I can do that using php and regex?

Regex would be ^\d{16}$
preg_match("/^\\d{16}$/", $str);

^\d{16}$
Explication:
^ for the beginning of the line (so nothing before that)
\d meaning a number (decimbal)
{16} to mean "exactly 16"
$ for the ending of the line (so nothing after that)

I think you need it for the credit card validator
^(\d{4}[- ]){3}\d{4}|\d{16}$
this will match :
1234-1234-1234-1234 | 1234 1234 1234 1234 | 1234123412341234
Please note that this is copied from regexlib

Would it not be better to make sure its 16 characters and numbers only with is_numeric() and strlen().

Related

How can I add optional validation patten in preg_match?

I'm try to use preg_match to parse the line. But, couldn't get the result correctly.
I want to parse those lines with optional // char.
Basic idea is some lines may contain //0016OIXXXXXXX (//4 digit and 9 str) and some line may not contain (//4 digit and 9 str).
Next line \n is mandatory.
Here are some sample lines;
Line 1
1812121212DD2220,31NTRFNONREF
502?102330
Line 2
1811091109CD20693,12NTRFRMSOIC110871941//0016RFXXXXXXX
206?000801
Here is my patten;
/(\d{6})((\d{2})(\d{2}))?(C|D)([A-Z]?)([0-9,]{1,15})([A-Z]{1,4})([A-Z a-z 0-9]{1,16})(\/\/)([0-9A-Z]{1,16})(\s*\n)([0-9]{1,3})(\?)([0-9]{0,6})/
Current patten can only work with //. If without //, patten is broken and can't parse the line.
I don't know how should I add (\/\/)([0-9A-Z]{1,16}) as optional patten.
Thanks in advance
:)
Just make the optional part well... optional:
\d{6}(?:\d{4})?[CD][A-Z]?[0-9,]{1,15}[A-Z]{1,4}[A-Za-z0-9 ]{1,16}(?:\/\/[0-9A-Z]{1,16})?\s*\n[0-9]{1,3}\?[0-9]{0,6}
// ^^^^^^^^^^^^^^^^^^^^^^^
I've removed the capture groups for lisibility, if you really want to keep them:
(\d{6})((\d{2})(\d{2}))?(C|D)([A-Z]?)([0-9,]{1,15})([A-Z]{1,4})([A-Za-z0-9 ]{1,16})(?:(\/\/)([0-9A-Z]{1,16}))?(\s*\n)([0-9]{1,3})(\?)([0-9]{0,6})
DEMO
If Am I understanding right your question.
Please, try to use or expression between the groups and validate any you case.
For example
`(pattern1) | (pattern-2) | ( (pattern3) | (pattern 4) )`
Please, try to consider using the | expression.

12 digit Regex for Phone number

I an trying to get a regex for a phone number with exactly 12 digits in the format: +############.
Code i am trying to use is ([+]?)\d{12}(\d{2})?$ but no luck.
Please help
This pattern will match exactly 12 digits after a plus sign:
/^\+\d{12}$/
What is your trailing optional (/d{2})? component doing in your pattern?
This is the same functionality without regex:
$phone='+012345678912';
if($phone[0]=='+' && strlen($phone)==13 && is_numeric(substr($phone,1))){
echo 'valid';
}else{
echo 'invalid';
}
// displays: valid
Try this:
^\+\d{12}(\d{2})?$
You needed to escape the plus sign
Regex101 Demo
Do we need to capture certain digits/sequences or are you just validating its a number with that format?
I use this online tool regex101 whenever I'm unsure of regex. It shows on the right exactly what you're capturing/checking which is very useful. Depending on your use case, I don't see how this regex doesn't work, please provide an example. Otherwise:
You're only capturing the + sign and the 2 digits after the initial 12.
You anchor to the end of the string and not the beginning
Both the + and 2 extra numerals are optional but you wanted to get the +############ exact?
I suggest you use \+(\d{12}) and avoid using anchors and capture groups you do not require.
If you want to support optional spaces between sets of three numbers, you would use this regex instead
^(\+)(\d{3}\s?){4}(\d{2})?$
where \s is a space character and ? means optional
https://regex101.com/r/nX5XnH/3 (demo)
+012 345 678 912 (ok)
+012345 678 912 (ok)
+012 345678912 (ok)
+01234567891244 (ok)
012345678913 (no mach - missing plus sign)

PHP - Find number between 2 Unicode characters

Simple problem but i sux at regular expressions so i need here ur help.
What do i need to type to find a number between two first signs: •
Find out its codes but it doenst help me much: http://www.fileformat.info/info/unicode/char/2022/index.htm
Do you know what should i type in for example preg_match function to make it work?
Example:
• 12345 • TESTTESTTEST
Example Output:
12345
Thanks in advance!
To match a specific Unicode code point, use \x{FFFF} where FFFF is the hexadecimal number of the code point you want to match. You can omit leading zeros in the hexadecimal number between the curly braces. Since \x by itself is not a valid regex token, \x{1234} can never be confused to match \x 1234 times. It always matches the Unicode code point U+1234. \x{1234}{5678} will try to match code point U+1234 exactly 5678 times.
Anyway, what you're probably looking for is something like this:
\x{2022} (\d*) \x{2022}
As for the (\d*) part, it basically means match any digit infinite times, and assign this bit of the pattern as a match (braces stand for capture groups)
Actually i found out a way to do it a bit easier.
I used preg_match() with $pattern = "/[0-9]{1,}/";
Huh xD

Check if a String with a only Number and space in PHP

I try check a string is number or not.
number format:
2222 or 22 22 or 1 2222 or 22323423
(with space before,midlle and after digits)
Is better I use regular expression?
So how can I change it to do that?
preg_grep('~^[0-9]$~'
or Is there any faster method?
You can use this regex:
^\d+( +\d+)*$
This will allow 1 or more spaces only in the middle of the number but not at start or end.

My regex for testing phone issue

I want to validate these phone number formats:
517123123
+48517123123
+48 517 123 123
(48)517123123
(48)517 123 123
517-123-123
48 517-123-123
48/517-123-123
48 517 123 123
I wrote this regex:
(\+?)+(((\(([0-9]+){2,2}\)))|(([0-9]+){2,2})?)+(\/?)+(\s?)+(([0-9]+){9,9}|([0-9]+){3,3}(\s|-){1,1}([0-9]+){3,3}(\s|-){1,1}([0-9]+){3,3})
The problem is that it's makes big numbers like 8978978979878978967 valid. Where is my mistake?
Looking at just the end of the regex, I see something that you seem to be doing in multiple places;
([0-9]+){3,3}
The + says at least one repeat of [0-9], which makes 1111111111111 a perfectly valid match. You then limit it to exactly 3 of those matches, which can still be a very long number.
If you want exactly 3 digits, remove the +.
may be you lost anchors.... however, use my regex ^(\+?(\(\d{2}\)|(\d{2})|(\d{2}[/ ])))?((\d{3} \d{3} \d{3})|(\d{3}-\d{3}-\d{3})|(\d{9}))$
At the moment I can't see what your regex is doing, there is too much superfluous stuff in it.
You have too many groups
You want to repeat optional characters!?
e.g.:
(\+?)+, you don't need a group around and you don't want to repeat that, so \+? is what you want here.
(\s?)+, do you want to say "0 or more whitespaces"? Then \s* is what you need.
When you write e.g. {9,9}, then you can remove one digit, {9} is the same.
You are nesting quantifiers, thats the place where you allow too many characters. You have multiple places, where you do ([0-9]+){9,9}, that means 1 or more digits and repeat that 9 times.

Categories