I would like to match the following pattern
1.XXXXXX.XXX.X
The combination must begin with a 1 and must contain at least a second number greater than 0 somewhere. Only numbers and points allowed.
So the following examples would be correct
1.000000.000.1
1.000500.000.0
1.020030.030.0
And the following examples would be incorrect
1.000000.000.0
1.0000.00.0
1.0000d0.020.0
What I have created so far
(?=^[1][\.][0-9]{6}[\.][0-9]{3}[\.][0-9]{1}$) // check pattern 1.XXXXXX.XXX.X
(?=^[1-9](?!0000000000$)[0-9][1-9]?\d+$) // check if input is greater than 10000000000
Unfortunately, the second statement does not work because of the points in the input. Is it possible to read the complete number and ignore the points or is there a better solution?
You may use
^1\.(?!(?:\.?0)+$)\d{6}\.\d{3}\.\d$
See the regex demo
The regex will fail the match if there are only zeros and dots after the initial 1..
Details
^1\. - 1. at the start of the string
(?!(?:\.?0)+$) - a negative lookahead that will fail the match of there are one or more sequences of an optional . and a zero up to the end of the string
\d{6} - 6 digits
\. - a dot
\d{3} - 3 digits
\. - a dot
\d - a digit
$ - end of string.
Related
I'm trying to find a regex for numeric inputs. We can receive a leading 0 just if we add a dot for adding 1 or 2 decimal numbers. And of course just accept numbers.
These are the scenarios that we can accept:
0.01
1.1
1.02
120.01
We can't accept these values
0023
0100
.01
.12
Which regex is the best option for these cases?
Until now we try we the following regex for accepting just number and dots
[A-Za-z,]
And also we try with the following ones:
^[+-]?[0-9]{1,3}(?:[0-9]*(?:[.,][0-9]{1})?|(?:,[0-9]{3})*(?:\.[0-9]{1,2})?|(?:\.[0-9]{3})*(?:,[0-9]{1,2})?)$
"/^[-]?[$]\d{1,3}(?:,?\d{3})*\.\d{2}$/"
"/(^(\d{1})\.{0,1}([0-9]){0,2}$)|(^([1-9])\d{0,2}(\,\d{0,3})$)/g"
(?:0|[1-9][0-9]*)(?:\.[0-9]{1,2})?
And the next one for deleting the leading zeros but it didn't work for 0.10 cases
^0+
If a negative lookahead is supported, you can exclude matches that start with a zero and have no decimal part.
^(?!0\d*$)\d+(?:\.\d{1,2})?$
^ Start of string
(?!0+\d*$) Negative lookahead, assert not a zero followed by optional digits at the right
\d+ Match 1+ digits
(?:\.\d{1,2})? Match an optional decimal part with 1 or 2 digits
$ End of string
Regex demo
I would go with ^(0|[1-9]\d*|(0|[1-9]\d*)\.\d+)$
You can test here: https://regex101.com/r/oNMgR9/1
Explanation
^ means : match the beginning of the string (or line if the m flag is enabled).
$ means : match the end of the string (or line if the m flag is enabled).
(a|b) means match "a" or match "b" so I'll use this to match either "0" alone or any number not starting with a "0". It's the syntax for a logical or.
. alone is used to match any char. So you have to escape it if you want to match the dot character. This is why I wrote 0\. instead of 0..
[ ] is used to list some characters you want to match. It can be a range if you use the - char, so [1-9] means any digit char from "1" to "9".
\d is to match a digit. It's totally equivalent to [0-9].
* means : match the preceding pattern 0 or many times, so \d* means that it will match 0 or many times a digit, so it will match "8" or "465" or "09" but also an empty string "". If you want to match the preceding pattern at least once or many times then you use + instead of *. So \d+ won't match an empty string "" but \d* would match it.
A) Just a number not starting with 0
[1-9]\d* will match any digit from 1 to 9 and then optionnaly followed by other digits. This will match numbers without a decimal point.
B) Just 0
0 alone is a possibility. This is because the case above isn't covering it.
B) A number with decimals
(0|[1-9]\d*)\.\d+ will match either a "0" alone or a number not starting by "0" and then followed by a point and some other digits (which have to be present because we don't want to match "45." without the numbers behind the dot).
Better alternative
The solution from #TheFourthBird is a bit cleaner with the use of a negative lookahead. It's just a bit different to understand. And he read the question completely: You wanted 1 or 2 digits after the decimal. I forgot about that, so, effectively, \d+ should be replaced by \d{1,2} as you don't want more than 2 digits.
You can use
^(?![0.]+$)(?:[1-9]\d*|0)(?:\.\d{1,2})?$
See the regex demo.
Details:
^ - start of string
(?![0.]+$) - fail the match if there are just zeros or dots till end of string
(?:[1-9]\d*|0) - either a non-zero digit followed with any zero or more digits or a zero
(?:\.\d{1,2})? - optionally followed with a sequence of a . and one or two digits
$ - end of string.
I need regular expression for numbers that can be negative, with commas or decimal.
For examples: 12 +12 -12.0 -12,345.5466 +12,345,678,678 0.154
But not pass: "500." or "500,".
I want that 500 will return as a number without the point or the comma that connect to the number.
The regular expression that I wrote is:
[-+]?((0|([1-9](\d*|\d{0,2}(,\d{3})*)))(\.\d*[0-9])?)(?!\S)
It is work at the correct numbers (like the examples), but It don't work for the '500,' or '500.'
Assuming you want to match 500 in 500. and 500,, you should bear in mind that (?!\S) requires a whitespace or end of string immediately to the right.
You may fix the problem with
[-+]?(?:0|[1-9](?:\d{0,2}(?:,\d{3})*|\d*))(?:\.\d+)?(?!\d)
See this regex demo, and note that this can be further enhanced depending on what contexts you need to exclude.
I replaced (?!\S) with (?!\d) at the end to fail the match if there is a digit, not any non-whitespace char, immediately on the right.
Note also that I removed unnecessary groups and converted all capturing groups to non-capturing.
Also, pay attention to the (?:,\d{3})*|\d*) group, where I swapped the alternatives since the first one is more specific and should go first.
Details
[-+]? - an optional - or +
(?:0|[1-9](?:\d{0,2}(?:,\d{3})*|\d*)) - 0 or
[1-9] - a non-zero digit
(?:\d{0,2}(?:,\d{3})*|\d*) - either
\d{0,2}(?:,\d{3})* - zero, one or two digits, and then zero or more occurrences of a comma and three digits
| - or
\d* - zero or more digits
(?:\.\d+)? - an optional sequence of . and one or more digits
(?!\d) - a negative lookahead that fails the match if there is a digit immediately to the right of the current location.
I am trying to block any strings that contain more than 3 numbers and prevent special characters. I have the special characters part down. I'm just missing the number part.
For example:
"Hello 1234" - Not Allowed
"Hello 123" - Allowed
I've tried the following:
/^[!?., A-Za-z0-9]+$/
/((^[!?., A-Za-z]\d)([0-9]{3}+$))/
/^((\d){2}[a-zA-Z0-9,.!? ])*$/
The last one is the closest I got as it prevents any special characters and any numbers from being entered at all.
I've looked through previous posts, but am coming up short.
Edit for clarification
Essentially I'm trying to find a way to prevent customers from entering PII on a form. No submission should be allowed that contains more than 3 numbers in a string.
Hello1234 - Not allowed
12345 - Not allowed
1111 - not allowed
No where in the comment section when the user enters the string should there be more than 3 numbers in total.
About the patterns that you tried
^[!?., A-Za-z0-9]+$ The pattern matches 1+ times any of the listed, including 1 or more digits
((^[!?., A-Za-z]\d)([0-9]{3}+$)) If {3}+ is supported, the pattern matches a single char from the character class, 1 digit followed by 3 digits
^((\d){2}[a-zA-Z0-9,.!? ])*$ The pattern repeats 0+ times matching 2 digits and 1 of the listed in the character class
You can use a negative lookahead if that is supported to assert not 4 digits in a row.
^(?!.*\d{4})[a-zA-Z0-9,.!? ]+$
regex demo
If there can not be 4 digits in total, but 0-3 occurrences:
^[a-zA-Z,.!? ]*(?:\d[a-zA-Z,.!? ]*){0,3}$
Explanation
^ Start of string
[a-zA-Z,.!? ]* Match 0+ times any of the listed (without a digit)
(?:\d[a-zA-Z,.!? ]*){0,3} Repeat 0 - 3 times matching a single digit followed by optional listed chars (Again without a digit)
$ End of string
regex demo
If you don't want to match an empty string and a lookahead is supported:
^(?!$)[a-zA-Z,.!? ]*(?:\d[a-zA-Z,.!? ]*){0,3}$
See another regex demo
Here is my two cents:
^(?!(.*\d){4})[A-Za-z ,.!?\d]+$
See the online demo
^ - Start string anchor.
(?! - Open a negative lookahead.
( - Open capture group.
.*\d - Match anything other than newline up to a digit.
){4} - Close capture group and match it 4 times.
) - Close negative lookahead.
[A-Za-z ,.!?\d]+ - 1+ Characters from specified class.
$ - End string anchor.
I think it should cover what you described.
Assuming you mean <= 3 digits, this may be a naive one but how about
[ALLOWED_CHARS]*[0-9]?[ALLOWED_CHARS]*[0-9]?[ALLOWED_CHARS]*[0-9][ALLOWED_CHARS]*?
Fill [ALLOWED_CHARS] to whatever you define is not special character and nums.
I have a specific pattern I'm trying to get. The pattern I'm looking for is the following: 13 digits with a possible dot for a total of min 3 and max 13 digits (including the dot if present) and ending with "/" and number from 1 to 6.
for now I have this pattern
^(\d*|\d*\.?\d*)\/[1-6]$
but this matches 1234/1 or 123456.890123456778/2
but it's not what I need
I tried a few things but I think I missing something
^(\d*|\d*\.?\d*){3-13}\/[1-6]$
Possible match:
1.3/1
123456./2
123456.890123/3
1234567890123/4
123/5
How do I solve this problem?
Your wordings are a little confusing but if I got you correct then you can use this regex,
^(?=.{5,15}$)\d+\.?\d*\/[1-6]$
Explanation:
^ - Start of string
(?=.{5,15}$) - This positive look ahead ensures that the minimum length is 5 and max length is 15 (adding two for last slash and number)
\d+\.?\d* - Starts capturing the text with one or more digits followed by optional dot . and further more zero or more digits
\/[1-6] - Matches a slash and one to six digit
$ - End of string
Regex Demo
Let me know if this works fine for you else list the case for which it doesn't work.
I am looking for help here. I want to write a regex to help me find EXACTLY a 7 digit in string - no more or less.
For instance in this string:
1234567 RE:TKT-2744870-R6P1G0: Gentle Reminder
It should return only 1234567
In this one:
12345678 RE:TKT-2744870-R6P1G0: Gentle Reminder
It should return none.
Can you help me with this one.
thanks in advance.
The proper regex should include \d{7} (7 digits) and 2 "border criteria",
for both start and end of the match, to block matching of a fragment
from longer sequence of digits.
My first thought was that neither before nor after the match there can be any digit.
But as I see from your example, these border criteria should be extended.
The set of "forbidden" chars (either before or after the match) should
include also - and letters.
E.g. 2744870 in your example data contains just 7 digits (no more, no less),
but you still don't want it to be matched, apparently because they are surrounded with - chars.
To keep the regex short, I propose:
(?<![\w-])\d{7}(?![\w-])
Details:
(?<![\w-]) - Negative lookbehind for word char or -.
\d{7} - 7 digits.
(?![\w-]) - Negative lookahead for word char or -.
If you decide to extend the set of "forbidden" chars in both border criteria,
just add them to [...] fragments in lookbehind / lookahead (but - char
should remain at the end, otherwise it must be quoted with \).
Regex like (\d{7})[^\d] (in other proposition) is wrong,
as it matches last 7 digits from any longer sequence of digits
(no "front border criterion").
It matches also both 2744870 (surronded with - chars), which are not
to be matched.
This one should do for your examples:
(\d{7})[^\d]
The first matching group contains the seven digits.
Alternatively –as suggested in the comments– you can use a negative lookahead to only match the seven digits and not require matching groups:
^\d{7}(?!\d)