Regular Expression, comma must follow 1 or 2 digits numeric - regex

I am trying to write a regular expression that ensure if there's a comma then the following text should be 1 or 2 digits numeric.
Here's what I have so far.
(^\d{0,2})+(,\d{0,2})*$
The works in most cases but it is considering the following as valid.
12,22,,,,,,,,,, and 12,22,,,,,,,,,,12,12
What did I do wrong? Thanks!

\d{0,2} means "between 0 and 2 digits". It should be \d{1,2}

You are matching 0 to 2 digits after the comma instead of 1 or 2
the following should do the trick
(^\d{1,2})+(,\d{1,2})*$

Use a negative look-ahead to assert that there aren't 3 digits after a comma, and keep the main regex simply "all commas or digits"
^(?!.*,\d{3})[,\d]+$

Related

Get numbers (including "-") with non leading zero

I want to match numbers including "-" with non leading zeros and normal numbers without "-". Therefore I want to use a regular expression.
The regex
should match 0 1 2 3 123 2-3 22-33 and
should not match 0123-123 01234.
The following regex works nearly:
\b(0|[1-9][0-9]*\-?[0-9]*)\b
The numbers 0 1 2 3 123 2-3 22-33 and 01234 are matched correctly, but 0123-123 not: it is matched partly. https://regex101.com/r/0Po3Ed/1.
You may use a negative lookbehind in your regex:
(?<!-)\b(?:0|[1-9][0-9]*(?:-[0-9]+)?)\b
Updated RegEx Demo
(?<!-) is negative lookbehind expressions that will fail the match if you have - before numbers.

Match a string with a word and a digit 1-9

My regex is weak, in the case of the following string
"OtherId":47
"OtherId":7
"MyId":47 (Match this one)
"MyId":7
I want to pick up the string that has "MyId" and a number that is not 1 - 9
I thought I could just use:
RegEx: How can I match all numbers greater than 49?
Combined using:
Regular Expressions: Is there an AND operator?
But its not happening... you can see my failed attempt here:
https://www.regextester.com/index.php?fam=99753
Which is
\b"MyId":\b(?=.*^[0-10]\d)
What am I doing wrong?
You can use this regex to match any digit >= 10:
^"MyId":[1-9][0-9]+$
RegEx Demo
If leading zeroes are to be allowed as well then use:
^"MyId":0*[1-9][0-9]+$
[1-9] makes sure number starts with 1-9 and [0-9]+ match 1 or more any digits after first digit.
Essentially, you are looking for 2 or more digits:
\"MyId\"\:(\d{2,})
I have escaped the quotes and colon, and {2,} means 2 or more.
If you need exact match to any number greater than 9
^"MyId":[1-9][0-9]+$

Regex to match a 2-digit number or a 3 digit number

I need to be able to check if a string contains either a 2 digit or a 4 digit number before a . (period).
For example, 39. is good, and so is 3926., but 392. is not.
I originally had (^\\d{2,4).$) but that allows between a 2 and a 4 digit number preceding a period.
I also tried (^\\d{2}.|\\d{4}.$) but that didn't work.
You can use this regex:
^\d{2}(?:\d{2})?\.$
This regex makes 2nd set of \d{2} optional thus allowing to match 12. or 1234. but not 123..
In the expression (^\d{2}.|\d{4}.$), the dots match any character.
Try escaping them to make them match literal dots: (^\d{2}\.|\d{4}\.$)

Regular Expression for 3 digit without 000

I want to write a regular expression on Google Form
First Character between 1 to 9
Second and Third any alphabets (Upper Case)
and next 3 characters should be number like 541 or 001 but not 000
This expression is also taking 000
[1-9][A-Z]{2}[0-9]{3}
Use alternations:
[1-9][A-Z]{2}([1-9][0-9][0-9]|[0-9][1-9][0-9]|[0-9][0-9][1-9])
See regex demo
Here,
[1-9] - matches 1 digit from 1 to 9
[A-Z]{2} - two uppercase ASCII letters
([1-9][0-9][0-9]|[0-9][1-9][0-9]|[0-9][0-9][1-9]) - 3 alternatives:
[1-9][0-9][0-9] - 3-digit numbers starting with 1
[0-9][1-9][0-9] - 3-digit numbers having 1 in the middle
[0-9][0-9][1-9] - 3-digit numbers ending with 1
Also, see this regex demo.
Use a negative look-ahead to avoid the triple zero at the end:
[1-9][A-Z]{2}(?!000)[0-9]{3}
Using the alternation operator
[1-9][1-9][1-9]|0[1-9][1-9]|00[1-9]|0[1-9]0

My regex for matching decimal numbers matches "1." How can I fix it?

I got this regular expression for decimals:
/^(\d{1,3})(\.{0,1}\d{0,2})$/
But It allows "1."
How could I fix this?
The following regex matches 1-3 digits, optionally followed by a decimal point and 1-2 digits.
/^(\d{1,3})(\.\d{1,2})?$/
Note that I also changed your . to \.. It is a metacharacter that matches anything, and so it has to be escaped.
/^(\d{1,3})(\.\d{1,2})?$/
I assume you're trying to match between 0 and 999.99, with 0, 1, or 2 decimals. If there are no decimals, you want no period separator. If that's the case, you want to the above.
For what it's worth, if you don't want to force a 0 at the beginning of the number, you can use the following expression:
(\d*\.)?\d+
This will match:
1
.1
1.1
This will not match:
1.
.
With some modification, this expression can still be used to force a certain number of digits:
(\d{1,3}\.)?\d{1,2}