regex patterns - noob in PHP - php

I have been testing and googling but still could'nt work out pattern to validate comma separated numbers.
9 digits long, numbers only, no spaces, leading number for each digit cannot be zero
Tried
^(?:\s*\d{9}\s*(?:,|$))+$
but no go
Note that commas are required since the input file should between 3 up to 20 (max) comma separated integers

You mentioned that there should be no spaces, but you used \s* (0+ whitespaces) in your pattern. Also, (?:,|$) matches a , or end of string, so your pattern allows a trailing ,.
I suggest using
^[1-9]\d{8}(?:,[1-9]\d{8}){2,19}$
See the regex demo
Details:
^ - start of string
[1-9] - the first digit of the 9-digit number cannot be zero, 1 to 9 only
\d{8} - the 8 remaining digits of the number
(?:,[1-9]\d{8}){2,19} - 2 to 19 (in total, 3 to 20) occurrences of
, - comma
[1-9]\d{8}){2,19} - see above
$ - end of string.

preg_match('=^[1-9][0-9,]*$=', $x); should do it, unless you're saying the number must be 9 digits long (with optional commas? or are the commas required?), in which case try something like preg_match('=^[1-9][0-9]{2},?[0-9]{3},?[0-9]{3}$=', $x);

Related

PHP Regex: allowed no more 3 digits but any count for another chars

I need to modify the exp:
/^[-\p{L}\d]+$/u
The purpose is to allow maximum 3 digits in whole string, but letter chars and dashes should be allowed without quantity restrictions. No matters where digits are located within string. For instance:
test345 //this should match
345te-st //this should match
3454dtest //this shouldn\'t match
I have already tried some patterns, but is don't work properly:
/^[-\p{L}\d{0,3}]+$/u
/^[-\p{L}]+[\d]{0,3}+$/u
/^([-\p{L}]+)([\d]+){0,3}$/u
The patterns that you tried don't work properly matching max 3 digits anywhere in the string because between the start and end anchors:
This notation is fully in a character class [-\p{L}\d{0,3}]+ which matches repeating 1+ times any of the listed characters between [...] (So there is no digit limit due to the +)
This notation [-\p{L}]+[\d]{0,3}+ matches 1+ times [-\p{L}] followed by 0-3 consecutive digits (So the digits can not be at the start for example)
This notation ([-\p{L}]+)([\d]+){0,3} uses 2 capture groups, but the order here is also 1+ times [-\p{L}] and 0-3 consecutive digits (only here is the capture group repeated)
If empty strings are also allowed:
^(?:[\p{L}-]*\d){0,3}[\p{L}-]*$
^ Start of string
(?:[\p{L}-]*\d){0,3} Match 0-3 times optional repetitions of [\p{L}-] and a single digit
[\p{L}-]* Match optional repetitions of [\p{L}-] at the end
$ End of string
Regex demo
Else you can assert that the string is not empty using (?!$)
^(?!$)(?:[\p{L}-]*\d){0,3}[\p{L}-]*$
Regex demo

Laravel - validate only digits with fixed length and required space

I am writing a regular expression to validate a zip code, where it should have exactly a length of 6 characters, the first 3 characters are digits, the last 2 also, but the character 4 should be a space.
Here some examples:
"123456" is not valid because no space in character 4.
"123 45" is valid.
"123 4" is not valid because the length is 5 instead of 6.
Here what I wrote:
/^[0-9 ]{6,6}$/
It works fine, just in this code above, the space is not required (but it should be).
You may use
/^\d{3} \d{2}$/
It matches 3 digits, space, 2 digits strings. See the regex demo.
Details
^ - start of string
\d{3} - 3 digits (\d matches an ASCII digit in PHP regex by default, same as [0-9])
- space
\d{2} - 2 digits
$ - end of string
Note that {6,6} limiting quantifier is the same as {6}.
You were close but you missed some things.
If you changed your regex to this, it should have worked.
^[0-9]{3}\s[0-9]{2}$
Or you could also use
^\d{3}\s\d{2}$
Note that when you say {6,6}, it means the preceding expression must occur between m and n times. You could just give it as {6}, although your regex is wrong, it's just something to know.
Hope it helps :)

RegularExpression with currency $ US or Canadian. Validating

^\$?(\d{1,3},?(\d{3},?)*\d{3}(\.\d{0,2})?|\d{1,3}(\.\d{0,2})?|\.\d{1,2}?)$
I actually found this to help me to validate the amount of $. The problem is that I want to have a limited amount to validate between 0$ and 99.99$. also amounts like 01.20 and 10.1 are not acceptable but 1.20$ 10.10 are.
Is there something I could modify on this regex.
Also this is for the use of my php code. I know I need to put one more backlash on the regex to make it work on php. thanks.
See regex in use here
^(?:\d{1,2}(?:\.\d{2})?|\.\d{2})\$$
^ Assert position at the start of the line
(?:\d{1,2}(?:\.\d{2})?|\.\d{2}) Match either of the following options
\d{1,2}(?:\.\d{2})? Option 1
\d{1,2} Match a digit one or two times
(?:\.\d{2})? Optionally match a decimal point followed by exactly two digits
\.\d{2} Option 2. Match a decimal point followed by exactly two digits
\$ Match $ literally
$ Assert position at the end of the line
Here is my suggestion:
^(?:0|[1-9]\d{0,2})(?:,?\d{3})*(?:\.\d{2})?\$$
^ asserts position at start of a line
(?:0|[1-9]\d{0,2}) matches either a 0 or a non-zero digit once followed by an optional digit
(?:,?\d{3})* matches an optional thousands separator followed by three digits zero or more times
(?:\.\d{2})? optionally matches a decimal place followed by two digits
\$ literally matches the dollar sign symbol
$ asserts position at the end of a line
Fiddle: Live Demo

regex for /command followed by number

Using the following regex doesn't work to validate /command number.
Here's what format of numbers I need to "validate"
/command 1
/command 0.5
/command 0.12345678
As you may see the value needs to be positive and the decimals are maximal 8.
I've done some research and found:
\/command\s?[\S]
But this work only for /command 1.
\/command\s?[\S] here [\S] will match only one non-space character so you can use and nothing else.
/command 1 // 1 : match one non-space
/command 0.5 // 0.5 :more than one non-space character so won't match
/command 0.123456789 // won't match
\/command\s?\S(\.\S{1,8})?
(\.\S+)? : ? match zero or one
(\.\S{1,8}) match . and 1 - 8 non-space character
more specifically for digits use
To define max 8 digit limit , use \d{1,8}
^\/command\s?\d(\.\d{1,8})?$
Note : if you want to match more digits before . e.g /command 123.5 then use
^\/command\s?\d+(\.\d{1,8})?$
as suggested by #jen and #serge
When you want to validate an entire string the first thing to remember is to enclose your pattern between the start and end of the string anchors: \A...\z
About the number with 8 decimals max there's nothing particular to say except that if you don't want a trailing dot you need to use an optional group and the correct quantifier: \d+(?:\.\d{1,8})?
Note also that you are free to change the pattern delimiter with an other character. This way you don't have to escape the slash that isn't a special regex character.
Result:
$pattern = '~\A/command \d+(?:\.\d{1,8})?\z~';
(feel free to make the space optional if needed)
This is because you missed '+' in [\S]+
But the above will not distinguish between numbers and other symbols.
To pick on 'numbers', you can use something like the following in 'perl'-like regex:
/\/command\s*[0-9.]+/

preg_match() or similar function for checking currency

1.449,00
1.000.000,55
19,90
etc
etc
I know what I listed above are very variable but there are possibilities for currency. I'm looking for a preg_match() example or any other function to deal with possible cases above. I tried with example below but it is not working properly. Any chance given me a most appropriate pattern for it?
if (! preg_match('/^[0-9.,]$/', $currency)) { echo 'No good.'; }
Something like this should work:
/^((?:\d{1,3}[,\.]?)+\d*)$/
This matches:
^ - Start of string
( - Capture the following:
(?:
\d{1,3} - One to three digits
[,\.]? - And an optional separator (comma or period)
)+ - One or more times
\d* - Zero or more digits
)
$ - End of string
It works by iteratively matching everything to the left side of the separator (if it's present), and then has \d* to pick up any optional fractions of currency.
You can see it passes all of your tests.
Edit: An updated regex looks like this:
^((?:\d\.\d{3}\.|\d{1,3}\.)?\d{1,3},\d{1,2})$
Where we match either:
\d\.\d{3}\. - One digit, a period, three digits, a period, OR
\d{1,3}\. - One and three digits, a period
None of the above (because of the ?)
Then, we match:
\d{1,3}, - One to three digits and a comma, followed by
\d{1,2} - One or two digits
Add + after the ] to allow more than just one character.

Categories