RegularExpression with currency $ US or Canadian. Validating - php

^\$?(\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

Related

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

PHP Pattern Validation

I'm having a bit of trouble getting my pattern to validate the string entry correctly. The PHP portion of this assignment is working correctly, so I won't include that here as to make this easier to read. Can someone tell me why this pattern isn't matching what I'm trying to do?
This pattern has these validation requirements:
Should first have 3-6 lowercase letters
This is immediately followed by either a hyphen or a space
Followed by 1-3 digits
$codecheck = '/^([[:lower:]]{3,6}-)|([[:lower:]]{3,6} ?)\d{1,3}$/';
Currently this catches most of the requirements, but it only seems to validate the minimum character requirements - and doesn't return false when more than 6 or 3 characters (respectively) are entered.
Thanks in advance for any assistance!
The problem here lies in how you group the alternatives. Right now, the regex matches a string that
^([[:lower:]]{3,6}-) - starts with 3-6 lowercase letters followed with a hyphen
| - or
([[:lower:]]{3,6} ?)\d{1,3}$ - ends with 3-6 lowercase letters followed with an optional space and followed with 1-3 digits.
In fact, you can get rid of the alternation altogether:
$codecheck = '/^\p{Ll}{3,6}[- ]\d{1,3}$/';
See the regex demo
Explanation:
^ - start of string
\p{Ll}{3,6} - 3-6 lowercase letters
[- ] - a positive character class matching one character, either a hyphen or a space
\d{1,3} - 1-3 digits
$ - end of string
You need to delimit the scope of the | operator in the middle of your regex.
As it is now:
the right-side argument of that OR runs up until the very end of your regex, even including the $. So the digits, nor the end-of-string condition do not apply for the left side of the |.
the left-side argument of the OR starts with ^, and only applies to the left side.
That is why you get a match when you supply 7 lowercase characters. The first character is ignored, and the rest matches with the right-side of the regex pattern.

php regex - find uppercase string with number and spaces in text

I want to write php regular expression to find uppercase string , which can also contain one number and spaces, from text.
For example from this text "some text to contain EXAM PL E 7STRING uppercase word" I want to get string- EXAM PL E 7STRING ,
found string should start and end only with uppercase, but in the middle, without uppercase letters can also contain(but not necessarily ) one number and spaces. So, regex should match any of these patterns
1) EXAMPLESTRING - just uppercase string
2) EXAMP4LESTRING - with number
3) EXAMPLES TRING - with space
4) EXAM PL E STRING - with more than one spaces
5) EXAMP LE4STRING - with number and space
6) EXAMP LE 4ST RI NG - with number and spaces
and with total length string should be equal or more than 4 letters
I wrote this regex '/[A-Z]{1,}([A-Z\s]{2,}|\d?)[A-Z]{1,}/', that can find first 4 patterns, but I can not figure it out to match also the last 2 patterns.
Thanks
There is a neat trick called a lookahead. It just checks what is following after the current position. That can be used to check for multiple conditions:
'/(?<![A-Z])(?=(?:[A-Z][\s\d]*){3}[A-Z])(?!(?:[A-Z\s]*\d){2})[A-Z][A-Z\s\d]*[A-Z]/'
The first lookaround is actually a lookbehind and checks that there is no previous uppercase letter. This is just a little speedup for strings that would fail the match anyway. The second lookaround (a lookahead) checks that there are at least four letters. The third one checks that there are no two digits. The rest just matches then a string of the allowed characters, starting and ending with an uppercase letter.
Note that in the case of two digits this will not match at all (instead of matching everything up to the second digit). If you do want to match in such a case, you could incorporate the "1 digit" rule into the actual match instead:
'/(?<![A-Z])(?=(?:[A-Z][\s\d]*){3}[A-Z])[A-Z][A-Z\s]*\d?[A-Z\s]*[A-Z]/'
EDIT:
As Ωmega pointed out, this will cause problems if there are less then four letters before the second digit, but more after that. This is actually quite tough, because the assertion needs to be, that there are more than 4 letters before the second digit. Since we do not know where the first digit occurs in those four letters, we have to check for all possible positions. For this I would do away with the lookaheads altogether, and simply provide the three different alternatives. (I will keep the lookbehind as an optimization for non-matching parts.)
'/(?<![A-Z])[A-Z]\s*(?:\d\s*[A-Z]\s*[A-Z]|[A-Z]\s*\d\s*[A-Z]|[A-Z]\s*[A-Z][A-Z\s]*\d?)[A-Z\s]*[A-Z]/'
Or here with added comments:
'/
(?<! # negative lookbehind
[A-Z] # current position is not preceded by a letter
) # end of lookbehind
[A-Z] # match has to start with uppercase letter
\s* # optional spaces after first letter
(?: # subpattern for possible digit positions
\d\s*[A-Z]\s*[A-Z]
# digit comes after first letter, we need two more letters before last one
| # OR
[A-Z]\s*\d\s*[A-Z]
# digit comes after second letter, we need one more letter before last one
| # OR
[A-Z]\s*[A-Z][A-Z\s]*\d?
# digit comes after third letter, or later, or not at all
) # end of subpattern for possible digit positions
[A-Z\s]* # arbitrary amount of further letters and whitespace
[A-Z] # match has to end with uppercase letter
/x'
That gives the same result on Ωmega's lengthy test input.
I suggest to use regex pattern
[A-Z][ ]*(\d)?(?(1)(?:[ ]*[A-Z]){3,}|[A-Z][ ]*(\d)?(?(2)(?:[ ]*[A-Z]){2,}|[A-Z][ ]*(\d)?(?(3)(?:[ ]*[A-Z]){2,}|[A-Z][ ]*(?:\d|(?:[ ]*[A-Z])+[ ]*\d?))))(?:[ ]*[A-Z])*
(see this demo).
[A-Z][ ]*(?:\d(?:[ ]*[A-Z]){2}|[A-Z][ ]*\d[ ]*[A-Z]|(?:[A-Z][ ]*){2,}\d?)[A-Z ]*[A-Z]
(see this demo)

What regex expression will check GPS values?

I'm letting users enter GPS values through a form, they all have the same form, some examples:
49.082243,19.302628
48.234142,19.200423
49.002524,19.312578
I want to check the entered value using PHP (using preg_match(), I guess), but as I'm not good in regex expressions (oh, dumb me, I should finally learn it, I know), I don't know how to write the expression.
Obviously it should be:
2x (numbers), 1x (dot), 6x (numbers), 1x (comma), 2x (numbers), 1x (dot), 6x (numbers)
Any suggestions how to write this in regex?
The other answers I see don't take into account that longitude goes from -180 to 180 and latitude goes from -90 to 90.
The proper regex for this would be (assuming the order is "latitude, longitude"):
/^(-?[1-8]?\d(?:\.\d{1,6})?|90(?:\.0{1,6})?),(-?(?:1[0-7]|[1-9])?\d(?:\.\d{1,6})?|180(?:\.0{1,6})?)$/
This regex covers having no less than -90 and no more than 90 for latitude as well as no less than -180 and no more than 180 for longitude while allowing them to put in whole numbers as well as any number of decimal places from 1 to 6, if you want to allow greater precision just change {1,6} to {1,x} where x is the number of decimal place
Also, if you capture on group 1 you get the latitude and a capture on group 2 gets the longitude.
Something like:
/^(-?\d{1,2}\.\d{6}),(-?\d{1,2}\.\d{6})$/
^ anchors at the start of input
-? allows for, but does not require, a negative sign
\d{1,2} requires 1 or 2 decimal digits
\. requires a decimal point
\d{6} requires exactly 6 decimal digits
, matches a single comma
(repeat the first 5 bullets)
$ anchors at the end of input
I have included capturing parentheses to allow you to extract the individual coordinates. Feel free to omit them if you don't need that.
All-around useful regex reference: http://www.regular-expressions.info/reference.html
/$-?\d{2}\.\d{6},-?\d{2}\.\d{6}^/
Expanding on the other answer:
/^-?\d\d?\.\d+,-?\d\d?\.\d+$/
Based on your example, this will do it:
if (preg_match('/(-?[\d]{2}\.[\d]{6},?){2}/', $coords)) {
# Successful match
} else {
# Match attempt failed
}
Explanation:
( # Match the regular expression below and capture its match into backreference number 1
- # Match the character “-” literally
? # Between zero and one times, as many times as possible, giving back as needed (greedy)
[\d] # Match a single digit 0..9
{2} # Exactly 2 times
\. # Match the character “.” literally
[\d] # Match a single digit 0..9
{6} # Exactly 6 times
, # Match the character “,” literally
? # Between zero and one times, as many times as possible, giving back as needed (greedy)
){2} # Exactly 2 times

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