I need a regX which can match like 123,123 only. My regX is
var regX = /^\d*[0-9](|.\d*[0-9]|,\d*[0-9])*$/;
but its currently matching 123,123 and 123, as well
Valid case: 123,123 or 123,000 or 000,000
Invalid case: 123.123 or 123?123 or '123'.'123'
you should use this regex = \d+(,\d+)+
You might want to use the {x,y} quantifier. I matches at least X of the item, and at most Y. If you leave one out, it has no limit in that direction. If you just have one number, with no comma it matches exactly that amount.
Exactly three digits:
(\d{3}),(\d{3})
Three or more
(\d{3,}),(\d{3,})
Between 2 and 7 digits:
(\d{2,7}),(\d{2,7})
And so on...
It looks like you're actually trying to match a number with thousand separators.
Try this: /\d{1,3}(?:,\d{3})*/
If your numbers are positive integers you can use: \d+,\d+
If you want floating point numbers as well: (\d|.)+,(\d|.)+
although this will also match malformed numbers with multiple or misplaced decimal points including .,. etc.
Related
I want to check if a string is a number. The accepted range of numbers in my case varies a lot from large numbers with a lot of decimals like;
100000000000000000.000000000000000001
1
25.9897
Above values should be matched!
Values that should not match are;
10,000.4
e19
How can I approach this?
^\d+(\.\d+)?$
A number with any length \d+, then maybe some optional decimal part (\.\d+)?
Also important to utilize line anchors ^ and $ to filter cases like e19
A possible problem can be a value like; 010.5, leading 0s can be kinda problematic, is that acceptable? Otherwise the way to filter values with trailing 0s out is to use; ^[1-9]\d*(\.\d+)?$. Just FYI
see it on regex101
I need a regular expression to validate a concatenated string that consists of 7 digit numbers separated by commas.
Furthermore, I must ensure that:
The string is not empty.
The chain doesn't begins or finish with commas.
The numbers do not start with 0.
Example: 1234567,2345678,3456789
My solution so far: ^\d+(,\d+)*?$
The problems I still need to resolve:
Validate that the numbers are exactly 7 digits.
Validate that the numbers do not start with 0.
Thank you.
Something like ^[1-9]\d{6}(,[1-9]\d{6})+$ should work. The [1-9] ensures the number doesn't begin with 0, and \d{6} ensures that there are 6 digits to follow.
Based on Gavin answer, here is what worked for me : ^[1-9]\d{6}(,[1-9]\d{6})*$
The minor difference is the use of the * instead of + at the end of the regular expression. There are some cases where I must validate only one 7 digits number...
Thank you for the help everyone!
I am looking for a regex string to match a set of numbers:
9.50 (numbers without spaces, that have 2 to 4 decimal points)
1 9 . 5 0 (numbers with spaces that have 2 to 4 decimals points)
10 (numbers without spaces and without decimal points)
So far I have come up regex string [0-9\s\.]+, but this not doing what I want. Any cleaner solutions out there?
Many Thanks
Try this:
[\d\s]+(?:\.(?:\s*\d){2,4})?
This makes the decimal point and the digits/spaces after it optional. If there are digits after, it checks that there are 2-4 of them with {2,4}
DEMO
If this should only match the whole string, you can anchor it.
^[\d\s]+(?:\.(?:\s*\d){2,4})?\s*$
The problem with your regex is that it will match 127.0.0.1 as well, which is an IP4 address, not a number.
The following regex should do the trick:
[0-9]+[0-9\s]*(\.(\s*[0-9]){2,4})?
Assumption I've made: You need to place at least one digit (before the comma).
regex101 demo.
(\d+[\d\s]*\.((\s*\d){2,4})?|\d+)
I was still getting "trailing spaces" selected with the third example of 10
This eliminated them.
wouldn't this work as well - '[^. 0-9]' ?
my full postgresql query looks like this:
split_part(regexp_replace(columnyoudoregexon , '[^. 0-9]', '', 'g'), ' ', 1)
and its doing the following:
values in the column get everything except numbers, spaces and point(for decimal) replaced with empty string.
split this new char string with split_part() and call which element in the resulting list you want.
was stuck on this for a while. i hope it helps.
I have a notepad with data that looks like this:
"$7.49"
"$124.00"
"$530.00"
How can I search through a range using regular expressions like 200-1000, but that values must be in "$XXX.XX" format.
Thanks for the help!
You can't easily manipulate numbers through regular expressions. It's doable though, so let's look at what you want to have.
Numbers are in the form \d+\.\d+, with no preceding zeroes.
You want to match numbers superior or equal to 200.
You want to match numbers lower or equal to 1000.
So, we have to look at our numbers like they are strings of characters. With the exception of 1000, all of those have three digits. So your regex is something like:
\$([2-9]\d\d\.\d\d|1000\.00)
That is, "a number with three digits left to the dot and the first one is 2 or higher or 1000.00".
This works with $XXX.XX \$\d+\.\d+. The problem with ranges are that numbers used in 3 digits bleed over to 4 digits and the regex gets way more complicated.
Use regex sets [ ] to limit the numbers/values.
So I would search for similar digits such as 200-999.xx which would be
\$[2-9]\d\d\.\d\d
or for four digits for 1000-1500
\$1[1-5]\d\d\.\d\d
If by range you mean number of digits, then go: \$\d{1,3}\,\d{1,2}
I have thousands of article descriptions containing numbers.
they look like:
ca.2760h3x1000.5DIN345x1500e34
the resulting numbers should be:
2760
1000.5
1500
h3 or 3 shall not be a result of the parsing, since h3 is a tolerance only
same for e34
DIN345 is a norm an needs to be excluded (every number with a trailing DIN or BN)
My current REGEX is:
[^hHeE]([-+]?([0-9]+\.[0-9]+|[0-9]+))
This solves everything BUT the norm. How can I get this "DIN" and "BN" treated the same way as a single character ?
Thanx, TomE
Try using this regular expression:
(?<=x)[+-]?0*[0-9]+(?:\.[0-9]+)?|[+-]?0*[0-9]+(?:\.[0-9]+)?(?=h|e)
It looks like every number in your testcase you want to match exept the first number is starting with x.This is what the first part of the regex matches. (?<=x)[+-]?0*[0-9]+(?:\.[0-9]+)?The second part of the regex matches the number until h or e. [+-]?0*[0-9]+(?:\.[0-9]+)?(?=h|e)
The two parts [+-]?0*[0-9]+(?:\.[0-9]+)? in the regex is to match the number.
If we can assume that the numbers are always going to be four digits long, you can use the regex:
(\d{4}\.\d+|\d{4})
DEMO
Depending on the language you might need to replace \d with [0-9].