problem with preg_match - php

I have to validate for a Laser Credit card. The card starts with either 6304, 6706, 6709, 6771 and is 16 or 19 digits in length. I have a preg_match and I am passing in the card number starting with 6706 and has 19 digits but it returns false.
// Laser (Laser) P: 6304, 6706, 6709, 6771 L: 16,19
} elseif (preg_match('/^(?:[6304|6706|6709|6771])\d{12,15}$/', $number)) {
$type = 'laser';

/^6(?:304|706|709|771)(?:\d{12}|\d{15})$/
Broken down:
/^ # start of line
6(?:304|706|709|771) # your 6xxx codes
(?:\d{12}|\d{15}) # 12 (16-4) or 15 (19-4) more numbers
$/ # end of pattern
To point out the mistakes you had:
(?:[6304|6706|6709|6771])
Remember that [] is a CLASS. That means to look for any of those characters within the brackets. If you're going for either/or, you need to use a group ().
Fixed it should look like: (?:6304|6706|6709|6771)
\d{12,15}
My understanding is you need fixed-length of numbers, not a variable one. Your quantifier is saying it can be 12, 13, ..., 15 more numbers. We only want 12 OR 15 more.

Related

Conditional regex length based on the first character

There is a string with numbers I need to validate with PHP preg_match.
If it starts with 10 or 20 or 30, I need 7 more numbers after the inital 2, but in any other cases I need 8 numbers only and don't care what are the lead characters.
The first part is the simple one
/^(1|2|3)0\d{7}$
But how can I add an ELSE part? There I need a simple
^\d{8}$
I need to match these examples:
101234567
201234567
12345678
33445566
You may use
^(?:[1-3]0\d{7}|(?![1-3]0)\d{8})$
See the regex demo
Details
^ - start of string
(?: - start of a non-capturing group:
[1-3]0\d{7} - 1, 2 or 3, then 0 and any 7 digits
| - or
(?![1-3]0)\d{8} - no 10, 20 or 30 immediately at the start of the string are allowed, then any 8 digits are matched
) - end of the group
$ - end of the string.
Here's an alternative using (?(?=regex)then|else) aka conditionals:
^(?(?=[1-3]0)[1-3]0\d{7}|\d{8})$
It literally says: if [1-3]0 is right at the start, match [1-3]0\d{7}, else match \d{8}.
Demo: https://regex101.com/r/LXoHyk/1 (examples shamelessly taken from Wiktor's answer)

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 on numbers, optional decimals and letter

I'm trying to "validate" some input from a form on the backend but I kinda got stuck with the required regex. Basically, I want to get a number, no leading zeroes, with an optional decimal and an optional case insensitive thousands notation (1,000 = 1k, 1,000,000 = 1m, 1 billion = 1b ...)
These should match/validate:
12.39
12.389k
99.1003b
40.1m
These should fail.
0.0
12.00b
1e3
2^5
0xFF
12.6z
asdf
So far I've tried this:
^[1-9]\d*(?:\.\d+)?(?:[kmbt])?$
It works but I'm thinking maybe someone could solve this a bit more elegantly than I did.
[Edit] without thousand separator:
/^(?:[1-9][0-9]*(?:\.[0-9]*[1-9])?|0\.[0-9]*[1-9])[kbmt]?$/i
demo
[Old answer] with thousand separator:
You can do it with:
/^(?:[1-9][0-9]{0,2}(?:(,?)[0-9]{3})?(?:\1[0-9]{3})*(?:\.[0-9]*[1-9])?|0\.[0-9]*[1-9])[kbmt]?$/i
demo
details:
^
(?: # numbers >= 1
[1-9][0-9]{0,2}
(?:(,?)[0-9]{3})? # capture an eventual thousand separator
(?:\1[0-9]{3})*
# decimal
(?:\.[0-9]*[1-9])?
| # numbers < 1
0\.[0-9]*[1-9]
)[kbmt]?$
Update in REGEX.
Below regex will not leading zero allow special characters and characters without specified for notation.
Below regex will allow decimal and integer with thousand and other notation.
^[1-9]\d*(\.[0-9]+)?(?:[kmbtKMBT])?$
Please check working demo : http://www.regexpal.com/?fam=95995
The below should be what you're after:
/(\b[1-9][\d\,\.]+([kmbt]?))(\s)/g
It matches 12.00b however... Here is an example of it's use.
Group 1 will contain your exact match.

How to extract an n digits number in different forms ( separated by a space) - PHP

I need to extract an 8 digit number from different text files. The issue is that there may be or may not a space before and after each digit as below.
Each client places the space however it thinks it makes the number more readable, hence the issue extracting it.
33113306
33 11 33 06
3311 3306
There may be numbers with more than 8 digits. Those should be ignored. E.g.
33 11 33 06 //the number we need
28 232392 93293293923 // the number we don't need
There is always a space before the first digit.
The 8 digit number we need is placed between 'words' or other characters. However sometimes we have new words on new lines. E.g.
This is a number
Al : 33 11 33 06 ,
Another number we don't need 232 2323 232 2, ..sdsad
I'm using PHP with preg_match and my old regex fails due this new "feature" to allow the client to define the number in different forms.
^(\d){8}$
You can use PHP's filter_var function, to filter out everything except numbers, plus and minus. You then use str_replace to remove the minus and plus (in case there is any):
$string=str_replace("+","",str_replace("-","",filter_var($string, FILTER_SANITIZE_NUMBER_INT)));
if(strlen($string)!=8){
echo "Error";
} else {
echo "Success";
}
You could just remove non-numeric characters, and get the first 8 characters of the string:
echo substr(preg_replace("/[^0-9,.]/", "", $string), 0, 8);
With your current input, you can use this:
[ ]\K(?<!\d )(?=(?: ?\d){8})(?!(?: ?\d){9})\d[ \d]+\d
See what matches and doesn't match in the regex demo.
[^ \d]+(\d\s*){8}[^ \d]+
Try this

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