regex for /command followed by number - php

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.]+/

Related

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 to get the first number after a certain string followed by any data until the number

I have a piece of data, retrieved from the database and containing information I need. Text is entered in a free form so it's written in many different ways. The only thing I know for sure is that I'm looking for the first number after a given string, but after that certain string (before the number) can be any text as well.
I tried this (where mytoken is the string I know for sure its there) but this doesn't work.
/(mytoken|MYTOKEN)(.*)\d{1}/
/(mytoken|MYTOKEN)[a-zA-Z]+\d{1}/
/(mytoken|MYTOKEN)(.*)[0-9]/
/(mytoken|MYTOKEN)[a-zA-Z]+[0-9]/
Even mytoken can be written in capitals, lowercase or a mix of capitals and lowercase character. Can the expression be case insensitive?
You do not need any lazy matching since you want to match any number of non-digit symbols up to the first digit. It is better done with a \D*:
/(mytoken)(\D*)(\d+)/i
See the regex demo
The pattern details:
(mytoken) - Group 1 matching mytoken (case insensitively, as there is a /i modifier)
(\D*) - Group 2 matching zero or more characters other than a digit
(\d+) - Group 3 matching 1 or more digits.
Note that \D also matches newlines, . needs a DOTALL modifier to match across newlines.
You need to use a lazy quantifier. You can do that by putting a question mark after the star quantifier in the regex: .*?. Otherwise, the numbers will be matched by the dot operator until the last number, which will be matched by \d.
Regex: /(mytoken|MYTOKEN)(.*?)\d/
Regex demo
You can use the opposite:
/(mytoken|MYTOKEN)(\D+)(\d)/
This says: mytoken, followed by anything not a number, followed by a number. The (lazy) dot-star-soup is not always your best bet. The desired number will be in $3 in this example.

matching 8 digit of alphanumeric in a string

I wanted to use regular expression to check if a string has a word that contains 8 digit of alphanumeric character, ignoring uppercase and lowercase (meaning that 2HJS1289 and 2hjs1289 should match). I know I can use preg to do this, and so far I have this:
preg_match('/[A-Za-z0-9]/i', $string)
I am unsure however on how to limit it only to 8 digits/character scheme.
For exactly 8 char word you will need to use word boundaries: \b
preg_match('/\b[A-Z\d]{8}\b/i', $string)
Try
preg_match('/\b([A-Z0-9]{8})\b/i', $string)
The {8} matches exactly 8 times. I added the capturing group (the parentheses), in case you needed to extract the actual match.
You can also use {min,max} to match the pattern repeating between min and max times (inclusive, I think). Or you can leave one of the parameters out to leave it open ended. Eg {min,} to match at least min times
[a-zA-Z0-9] - will match upper or lowercase letters or numbers
{8} - will specify to match 8 of the preceeding token
put it together:
preg_match('/([A-Za-z0-9]{8})/i', $string)
example

PHP simple regex

I want to validate a string only if it contains '0-9' chars with length between 7 and 9.
What I have is [0-9]{7,9} but this matches a string of ten chars too, which I don't want.
Thanks.
You'll want to use ^[0-9]{7,9}$.
^ matches the beginning of the string, while $ matches the end.
If you want to find 7-9 digit numbers inside a larger string, you can use a negative lookbehind and lookahead to verify the match isn't preceded or followed by a digit
(?<![0-9])[0-9]{7,9}(?![0-9])
This breaks down as
(?<![0-9]) ensure next match is not preceded by a digit
[0-9]{7,9} match 7-9 digits as you require
(?![0-9]) ensure next char is not a digit
If you simply want to ensure the entire string is a 7-9 digit number, anchor the match to the start and end with ^ and $
^[0-9]{7,9}$

Categories