Generic regex with format specific conditions - regex

Can anyone help me with a generic regex (in Visual Basic) that can handle below formats?
2100
2.100
2,100
2 100
2 100 (double white-spaces between "2" and "1"
10100
10.100
10,100
10 100
10 100
The Regex shall match all numbers in above formats not only the 2100 and 10100 examples.
b) also a generic Regex that matched above but dont match formats:
2.10
2,10
2.1
2,1
10.1
10,1
10.10
10,10
The regex I have tried but wont work is:
Regex(\d+(?:[,.]| {1,2})\d+$)

How about this:
^\d+(([.,]|\s{1,2})\d+)?$
Notice ^ and (([.,]|\s{1,2})\d+) which I made it optional with a ?

Related

Regex with a maximum value

I have the following RegEx:
(^\d{1,3}$)|(\d{1,3})\.?(\d{0,0}[0,5])
This accepts any .5 increment of a number.
I want to add a range to this number of 13.5. Ideally, .5 should be valid to.
So, anything from .5 to 13.5, in .5 increments.
Examples allowed:
.5
4
12.5
13.5
Not allowed:
.56
3.45
14
14.5
Your current regex has some big issues e.g. [0,5] doesn't mean either 0 or 5 but a 0, , or 5. Try the following regex instead:
^(?=.)(?:\d|1[0-3])?(?:\.5)?$
See live demo here
Based on your given allowed examples it matches 12 but doesn't match 12.0. If you want to match 12.0 as well you have to replace \.5 with \.[05]:
^(?=.)(?:\d|1[0-3])?(?:\.[05])?$
Note: postive lookahead (?=.) ensures that an empty match doesn't occur.
^([0-9]|1[0-3])?(\.[05])?$
This works for your given examples

Regex Finding Correct Decimal Number 1.0 OR 1.99 To 10.0

I'm trying to find a number that is between 1.0 or 1.10 but wont be higher than 10.0
Here is what I have so far,
^0$|^[1-9]{1}\.[0-9]{2}$|^10\.0$
this one is working for 1.55 I tried adding an OR to the expression, to find eg 1.5 but unfortunately it will not find 1.55 it does however continute to find 1.5Here is my attempt at the or statement
^0$|^[1-9]{1}\.[0-9]{1}$|[1-9]{1}\.[0-9]{2}$^10\.0$
Here is the site that I'm trying this with.
You forgot the alternation between [0-9]{2}$ and ^10, if you add | as your begining regex did, it should work.
^0$|^[1-9]{1}\.[0-9]{1}$|[1-9]{1}\.[0-9]{2}$|^10\.0$
Besides, {1} here is not required, because it's repeated only one time.
This regex is shorter:
^0$|^[1-9]\.[0-9]$|[1-9]\.[0-9]{2}$|^10\.0$
You can use the following regex that accept only numbers with max 2 decimals:
^0$|^[1-9](?:\.[0-9]{1,2})?$|^10(?:\.00?)?$
It will also accept 10.00 and all integers without decimals from 0 to 10.
Demo: https://regex101.com/r/tT1dX7/19
If you want to add numbers with maximum 2 decimals that are between 0 and 1 to your initial range (1-10 -> 0-10)
^[0-9](?:\.[0-9]{1,2})?$|^10(?:\.00?)?$
Demo: https://regex101.com/r/tT1dX7/20
Now if you want to accept more than 2 decimals in your range (0-10)
^[0-9](?:\.[0-9]+)?$|^10(?:\.0+)?$
Demo: https://regex101.com/r/tT1dX7/21
Maybe ^(?:1\.(?:0\d|[1-9]\d)|(?:[2-9]|10)\.00)$
^
(?:
# 1.00 - 1.99
1
\.
(?:
0 \d
| [1-9] \d
)
|
# 2.00 - 10.00
(?:
[2-9]
| 10
)
\.00
)
$

I used a regular expression to restrict the numbers only between 3.00 to 100.00.But it was allowing the numbers between 100.01 to 100.99

I have a requirement to restrict the numbers between 3.00 to 100.00
I used below expression
^([3-9]|[1-9][0-9]|100)+(\.\d{1,2})?$
The issue with above expression is that, it's allowing 100.01 to 100.99,which should be restricted.It also allows 310 to 399,which needs to restricted.
I used another flavor of same expression
^([3-9]|[1-9][0-9]|100.00)+(\.\d{1,2})?$
Which was working as expected,but we need to enter 100.00 in to pass the regular expression instead of 100.
Is there any way,I can achieve the desired result?
When alternating with the final 100, use negative lookahead for \.\d?[1-9], to ensure that the decimal places, if any, have only 0s.
Your first pattern can also match many repeated digits before the optional decimal (like 333 and 101010) due to the + at the end of the group, so best to remove the + if you only want to match between 3 and 100.
^(?:[3-9]|[1-9][0-9]|100(?!\.\d?[1-9]))(?:\.\d{1,2})?$
^^^^^^^^^^^^^^
https://regex101.com/r/tJd3LQ/1
To permit leading zeros, add 0* right after the ^:
^0*(?:[3-9]|[1-9][0-9]|100(?!\.\d?[1-9]))(?:\.\d{1,2})?$
^^
You can try this mate
^(?:100(?:\.0+)?|(?:[3-9]|[1-9][0-9])(?:\.\d{1,2})?)$
Demo
Explanation
^ - Anchor represent start of string.
(?:100(?:\.0+)?) - This will match 100, 100.0, 100.00(any number of decimal zero's).
| - Alternation this works same as Logical OR.
(?:[3-9]|[1-9][0-9])(?:\.\d{1,2})?) - This will match any number from 3.00 to 99.99
Suggestion
Always use non capturing group in case you're not using the group any where else again in your regex.
The problem with this: ^([3-9]|[1-9][0-9]|100)+(\.\d{1,2})?$, is that it also allows 100 in the first chunk (the chunk responsible for allowing whole numbers).
In your case, you would need to use something like so: ^100\.00$|([3-9]|[1-9][0-9])+(\.\d{1,2})?$ (Example here).
This expression will either try to match 100.00 as a whole (which is your upper bound, or else, any number between 3.00 up till 99.99.
Maybe this regex will work for You:
^([3-9]|[1-9][0-9])(\.[0-9]+)?$|^100(\.0+)?$
Test:
$ cat numeric.txt
0.0
3
3.0
3.00
3.001
2.99
99.99
99.999
100
100.0
100.00
100.01
100.99
$ egrep '^([3-9]|[1-9][0-9])(\.[0-9]+)?$|^100(\.0+)?$' numeric.txt
3
3.0
3.00
3.001
99.99
99.999
100
100.0
100.00

Regex for MoneyFormat w/ (,) and (.) Optionals

Hy guys i need to implement a money format data validation, i need this regex only accept the following formats:
Note: Only accept two or one positions for decimal before dot, the commas are optional every 3 digits
1
1.0
1.00
100.00
1,000.00
1000.00
111,000.00
111000.00
999,999,999.00
Assuming the Dot and commas are optionals.
Wrong Formats:
,1.00
1.
1,,00.00
1.000
etc
This one can help:
^\d+(?:,\d+)*(?:\.\d|\.\d\d)?$
See demo

How can I use RegEx to differentiate between screen resolutions?

I'm trying to create a RegEx expression that will match a numeric range from 0 to 600 so I can easily differentiate between a small mobile device and tablets/desktops. I'm using Qualtrics' survey software to do the rest - all I need is the RegEx expression.
However, I'm not 100% sure how Qualtrics takes in the data. I believe it takes it in the following format:
360x640
320x568
320x480
1920x1080
360x640
1280x800
320x568
1920x1080
360x640
1280x800
1920x1080
480x800
320x480
1280x800
1366x768
320x568
1280x800
Where I'm testing the FIRST number, e.g. the number before the 'x' character.
Here's some RegEx I've tried that did not work:
([0-9]{1,2}|[1-4][0-9]{2}|600)*x
That code recognizes numbers before the 'x', but it doesn't stop at 600 - it recognizes all numbers before the 'x' (e.g. from 000 to 9999).
How do I get the range I want? Please and thank you!
Note: I've tried using the RegEx number range generator here, but it doesn't work for what I want to accomplish.
I'd do:
\b(?:600|[1-9]\d?|[1-5]\d{2})x
Where:
\b is a word boundary, it makes sure there're no digits before
(?: ) is a non capture group
600 matches 600
[1-9]\d? matches number from 1 to 99
[1-5]\d{2} matches number from 100 to 599
I don't believe there are width lower than 100, so you can use this pattern:
^([1-5][0-9]{2}|600)x
You can use this regex but with m modifier if your input contains all those lines together:
^([0-5]\d{2}|600)x
Live demo