Using JavaScript, I need to accept only numbers and commas.
The regex pattern I am using is as follows
var pattern = /^[-+]?[0-9]+(\.[0-9]+)?$/;
How do I accept commas in the above pattern So that values like 3200 or 3,200 or 3,200.00 and so are valid?
There are similar questions that only partially deal with this:
Regex validation for numbers with comma separator (only whole numbers with no fractional part)
Decimal number regular expression, where digit after decimal is optional (no comma separation, fractional part limited to 1 digit)
Javascript function need allow numbers, dot and comma (the dots, commas and digits are matched in any order)
Use the following regex:
^[-+]?(?:[0-9]+,)*[0-9]+(?:\.[0-9]+)?$
See regex demo
The basic change here is the addition of (?:[0-9]+,)* subpattern that matches:
[0-9]+ - 1 or more digits
, - a comma
0 or more times (thanks to * quantifier).
I also used non-capturing groups so that regex output is "cleaner".
If you need to check for 3-digit groups in the number, use
^[-+]?[0-9]+(?:,[0-9]{3})*(?:\.[0-9]+)?$
See another demo
Here, (?:,[0-9]{3})* matches 0 or more sequences of a comma and 3-digit substrings ([0-9]{3}). {3} is a limiting quantifier matching exactly 3 occurrences of the preceding subpattern.
Related
The sample i wanna past through the regex is with follow requirement
list of number seperate by comma
at least input 10 number in list
only accept numbers but could allow empty space in both front and back
Here is my sample regex code:
^(?:(\-|\+|)\d+(?:\.\d*)?|(\-|\+|)\.\d+)+(?:,(?:(\-|\+|)\d+(?:\.\d*)?|(\-|\+|)\.\d+)){9,}$
This regex test code could pass list number seperate by comma,
however when i add empty space in front and on the back it will not work
The following testing Code is failed
' 2,2.5,.5, .678 ,39,1.4.4.8,2.4,2.5,2.6,2.7'
You might use
^\s*[+-]?\d*\.?\d+(?:\.\d+)*(?:\s*,\s*[+-]?\d*\.?\d+(?:\.\d+)*){9,}\s*$
The pattern matches:
^ Start of string
\s*[+-]?\d*\.?\d+ Match an optional plus or minus sign, optional digits and optional dot followed by 1 or more digits
(?:\.\d+)* As there are digits with multiple dot parts, you can optionally repeat that
(?:\s*,\s*[+-]?\d*\.?\d+(?:\.\d+)*){9,} That first part of the pattern matches 1 time. As you want to match at least 10 times you can repeat the first pattern 9 or more times, starting with a comma between optional whitespace chars
\s*$ Optional trailing whitespace chars and assert end of string
Regex demo
Note that \s can also match a newline.
If you don't want that, you could use for example a mere space or [ \t]
I'm attempting to string match 5-digit coupon codes spread throughout a HTML web page. For example, 53232, 21032, 40021 etc... I can handle the simpler case of any string of 5 digits with [0-9]{5}, though this also matches 6, 7, 8... n digit numbers. Can someone please suggest how I would modify this regular expression to match only 5 digit numbers?
>>> import re
>>> s="four digits 1234 five digits 56789 six digits 012345"
>>> re.findall(r"\D(\d{5})\D", s)
['56789']
if they can occur at the very beginning or the very end, it's easier to pad the string than mess with special cases
>>> re.findall(r"\D(\d{5})\D", " "+s+" ")
Without padding the string for special case start and end of string, as in John La Rooy answer one can use the negatives lookahead and lookbehind to handle both cases with a single regular expression
>>> import re
>>> s = "88888 999999 3333 aaa 12345 hfsjkq 98765"
>>> re.findall(r"(?<!\d)\d{5}(?!\d)", s)
['88888', '12345', '98765']
full string: ^[0-9]{5}$
within a string: [^0-9][0-9]{5}[^0-9]
Note: There is problem in using \D since \D matches any character that is not a digit , instead use \b.
\b is important here because it matches the word boundary but only at end or beginning of a word .
import re
input = "four digits 1234 five digits 56789 six digits 01234,56789,01234"
re.findall(r"\b\d{5}\b", input)
result : ['56789', '01234', '56789', '01234']
but if one uses
re.findall(r"\D(\d{5})\D", s)
output : ['56789', '01234']
\D is unable to handle comma or any continuously entered numerals.
\b is important part here it matches the empty string but only at end or beginning of a word .
More documentation: https://docs.python.org/2/library/re.html
More Clarification on usage of \D vs \b:
This example uses \D but it doesn't capture all the five digits number.
This example uses \b while capturing all five digits number.
Cheers
A very simple way would be to match all groups of digits, like with r'\d+', and then skip every match that isn't five characters long when you process the results.
You probably want to match a non-digit before and after your string of 5 digits, like [^0-9]([0-9]{5})[^0-9]. Then you can capture the inner group (the actual string you want).
You could try
\D\d{5}\D
or maybe
\b\d{5}\b
I'm not sure how python treats line-endings and whitespace there though.
I believe ^\d{5}$ would not work for you, as you likely want to get numbers that are somewhere within other text.
I use Regex with easier expression :
re.findall(r"\d{5}", mystring)
It will research 5 numerical digits. But you have to be sure not to have another 5 numerical digits in the string
I have the following filtered:
2 digits (?=..*\d)
2 uppercase characters (?=..*[a-z])
2 lowercase characters (?=..*[A-Z])
10 to 63 characters .{10,63}$
Which translates to:
(?=.{2,}\d)(?=..*[a-z])(?=..*[A-Z]).{10,63}
Then I want to exclude a word starting with the letter u, and ending with three to six digits:
([uU][0-9]{3,6})
However, how can I merge these two patterns to do the following:
It should not allow the following because it respectively:
# does not have the required combination of characters
aaaaaaaaaaaaaaa
# is too long
asadsfdfs12BDFsdfsdfdsfsdfsdfdsfdsfdfsdfsdfsdfsdsfdfsdfsdfssdfdfsdfssdfdfsdfssdfdfsdfsdfsdfsdfsfdsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfs
# contains the pattern that shouldn't be allowed
U0000ABcd567890
ABcd56U00007890
D4gF3U432234
D4gF3u432234
ABcd567890U123456
should allow the following:
# it has the required combination of characters
ABcd5678990
ABcd567890
# does contain a part of the disallowed pattern (`([uU][0-9]{3,6})`), but does not fit that pattern entirely
ABcd567890U12
ABcd5U12abcdf
s3dU00sDdfgdg
ABcd56U007890
Created and example here: https://regex101.com/r/4b2Hu9/3
In your pattern you make use of a lookahead (?=..*\d) which has a different meaning than you assume.
It means if what is directly on the right is 2 or more times any char except a newline followed by a single digit and the same for the upper and lowercase variants.
You could update your pattern to:
^(?!.*[uU]\d{3,6})(?=(?:\D*\d){2})(?=(?:[^a-z]*[a-z]){2})(?=(?:[^A-Z]*[A-Z]){2}).{10,63}$
In parts
^ Start of string
(?!.*[uU]\d{3,6}) Negative lookahead, assert not u or U followed by 3-6 digits
(?=(?:\D*\d){2}) Assert 2 digits
(?=(?:[^a-z]*[a-z]){2}) Assert 2 lowercase chars
(?=(?:[^A-Z]*[A-Z]){2}) Assert 2 uppercase chars
.{10,63} Match any char except a newline 10-63 times
$ End of string
Regex demo
First, the way to ensure that the string contains, for example, two digits would be to use a positive lookahead:
(?=.*\d.*\d)
You can generalize this to your other filters.
To make sure the string contains 10 - 63 characters:
.{10,63}
You say you do not want the string to begin with u or U followed by 3 to 6 digits (presumbaly 7 digits is okay), use a negative lookahead:
(?![uU]\d{3,6}\D)
The \D is required to make sure that if there is a 7th digit, then the string will be accepted.
Putting it all together:
r'^(?=.*\d.*\d)(?=.*[a-z].*[a-z])(?=.*[A-Z].*[A-Z])(?![uU]\d{3,6}\D).{10,63}$'
I have a regex expression like this
/^[0-9]*\s*kg\s*[0-9]*$/
This accepts strings like 100 kg or 100kg. I am tring to make it also accept 100.1kg or 100.1 kg or 10,20 kg and so on (both dot and decimal should be accepted). How do I change that current regex so that those numbers I mentioned also tests true? I have seen this regex which supports numbres and decimals but I am unsure how to combine it with my regex.
^[0-9]{1,2}([,.][0-9]{1,2})?$
To make it optional you could use an optional non capturing group (?: and add matching 0+ times a whitespace after it:
^[0-9]+(?:[,.][0-9]+)?\s*kg$
Regex demo
Now it will match
^ Start of the string
[0-9]+ 1+ digits
(?: Non capturing group
[,.][0-9]+ match comma or dot and 1+ digits
)? Close non capturing group and make it optional
\s*kg Match 0+ times a whitespace character
$ End of the string
For incorporating decimal values optionally in addition to whole number values, you can just place this regex (?:[.,]\d+)? after ^[0-9]* which will allow it to support decimal values like you mentioned in your post. Overall regex becomes this,
^\d*(?:[.,]\d+)?\s*kg\s*$
Demo
I am trying to get a regular expression to work but am stumped. What I want is to do the inverse of this:
/(\w)\1{5,}/
This regex does the exact opposite of what I'm trying to do. I would like to get everything but a string that has 6 repeating numbers i.e. 111111 or 999999.
Is there a way to use a negative look-around or something with this regex?
You can use this rgex:
/^(?!.*?(\w)\1{5}).*$/gm
RegEx Demo
(?!.*?(\w)\1{5}) is a negative lookaahead that will fail the match if there are 6 consecutive same word characters in it.
I'd rather go with the \d shorthand class for digits since \w also allows letters and an underscore.
^(?!.*(\d)\1{5}).*$
Regex explanation:
^ - Start of string/line anchor
(?!.*(\d)\1{5}) - The negative lookahead checking if after an optional number of characters (.*) we have a digit ((\d)) that is immediately followed with 5 identical digits (\1{5}).
.* - Match 0 or more characters up to the
$ - End of string/line.
See demo. This regex will allow