To match any number from 0 to 9 we use \d in regex. It will match any single digit number from 0 to 9. \d means [0-9] or match any number from 0 to 9. Instead of writing 0123456789 the shorthand version is [0-9] where [] is used for character range.
A regular expression for a decimal number needs to checks for one or more numeric characters (0-9) at the start of the string, followed by an optional period, and then followed by zero or more numeric characters (0-9). This should all be followed by an optional plus or minus sign.
Occurrence Indicators (or Repetition Operators): +: one or more ( 1+ ), e.g., [0-9]+ matches one or more digits such as '123' , '000' . *: zero or more ( 0+ ), e.g., [0-9]* matches zero or more digits. It accepts all those in [0-9]+ plus the empty string.
matches zero or one occurrence of the one-character regular expression. So to match any series of zero or more characters, use " . * ".
#ALL THESE REQUIRE THE WHOLE STRING TO BE A NUMBER
#For numbers embedded in sentences, see discussion below
#### NUMBERS AND DECIMALS ONLY ####
#No commas allowed
#Pass: (1000.0), (001), (.001)
#Fail: (1,000.0)
^\d*\.?\d+$
#No commas allowed
#Can't start with "."
#Pass: (0.01)
#Fail: (.01)
^(\d+\.)?\d+$
#### CURRENCY ####
#No commas allowed
#"$" optional
#Can't start with "."
#Either 0 or 2 decimal digits
#Pass: ($1000), (1.00), ($0.11)
#Fail: ($1.0), (1.), ($1.000), ($.11)
^\$?\d+(\.\d{2})?$
#### COMMA-GROUPED ####
#Commas required between powers of 1,000
#Can't start with "."
#Pass: (1,000,000), (0.001)
#Fail: (1000000), (1,00,00,00), (.001)
^\d{1,3}(,\d{3})*(\.\d+)?$
#Commas required
#Cannot be empty
#Pass: (1,000.100), (.001)
#Fail: (1000), ()
^(?=.)(\d{1,3}(,\d{3})*)?(\.\d+)?$
#Commas optional as long as they're consistent
#Can't start with "."
#Pass: (1,000,000), (1000000)
#Fail: (10000,000), (1,00,00)
^(\d+|\d{1,3}(,\d{3})*)(\.\d+)?$
#### LEADING AND TRAILING ZEROES ####
#No commas allowed
#Can't start with "."
#No leading zeroes in integer part
#Pass: (1.00), (0.00)
#Fail: (001)
^([1-9]\d*|0)(\.\d+)?$
#No commas allowed
#Can't start with "."
#No trailing zeroes in decimal part
#Pass: (1), (0.1)
#Fail: (1.00), (0.1000)
^\d+(\.\d*[1-9])?$
Now that that's out of the way, most of the following is meant as commentary on how complex regex can get if you try to be clever with it, and why you should seek alternatives. Read at your own risk.
This is a very common task, but all the answers I see here so far will accept inputs that don't match your number format, such as ,111
, 9,9,9
, or even .,,.
. That's simple enough to fix, even if the numbers are embedded in other text. IMHO anything that fails to pull 1,234.56 and 1234—and only those numbers—out of abc22 1,234.56 9.9.9.9 def 1234
is a wrong answer.
First of all, if you don't need to do this all in one regex, don't. A single regex for two different number formats is hard to maintain even when they aren't embedded in other text. What you should really do is split the whole thing on whitespace, then run two or three smaller regexes on the results. If that's not an option for you, keep reading.
Considering the examples you've given, here's a simple regex that allows pretty much any integer or decimal in 0000
format and blocks everything else:
^\d*\.?\d+$
Here's one that requires 0,000
format:
^\d{1,3}(,\d{3})*(\.\d+)?$
Put them together, and commas become optional as long as they're consistent:
^(\d*\.?\d+|\d{1,3}(,\d{3})*(\.\d+)?)$
The patterns above require the entire input to be a number. You're looking for numbers embedded in text, so you have to loosen that part. On the other hand, you don't want it to see catch22
and think it's found the number 22. If you're using something with lookbehind support (like .NET), this is pretty easy: replace ^
with (?<!\S)
and $
with (?!\S)
and you're good to go:
(?<!\S)(\d*\.?\d+|\d{1,3}(,\d{3})*(\.\d+)?)(?!\S)
If you're working with JavaScript or Ruby or something, things start looking more complex:
(?:^|\s)(\d*\.?\d+|\d{1,3}(?:,\d{3})*(?:\.\d+)?)(?!\S)
You'll have to use capture groups; I can't think of an alternative without lookbehind support. The numbers you want will be in Group 1 (assuming the whole match is Group 0).
I think that covers your question, so if that's all you need, stop reading now. If you want to get fancier, things turn very complex very quickly. Depending on your situation, you may want to block any or all of the following:
Just for the hell of it, let's assume you want to block the first 3, but allow the last one. What should you do? I'll tell you what you should do, you should use a different regex for each rule and progressively narrow down your matches. But for the sake of the challenge, here's how you do it all in one giant pattern:
(?<!\S)(?=.)(0|([1-9](\d*|\d{0,2}(,\d{3})*)))?(\.\d*[1-9])?(?!\S)
And here's what it means:
(?<!\S) to (?!\S) #The whole match must be surrounded by either whitespace or line boundaries. So if you see something bogus like :;:9.:, ignore the 9.
(?=.) #The whole thing can't be blank.
( #Rules for the integer part:
0 #1. The integer part could just be 0...
| #
[1-9] # ...otherwise, it can't have leading zeroes.
( #
\d* #2. It could use no commas at all...
| #
\d{0,2}(,\d{3})* # ...or it could be comma-separated groups of 3 digits each.
) #
)? #3. Or there could be no integer part at all.
( #Rules for the decimal part:
\. #1. It must start with a decimal point...
\d* #2. ...followed by a string of numeric digits only.
[1-9] #3. It can't be just the decimal point, and it can't end in 0.
)? #4. The whole decimal part is also optional. Remember, we checked at the beginning to make sure the whole thing wasn't blank.
Tested here: http://rextester.com/YPG96786
This will allow things like:
100,000
999.999
90.0009
1,000,023.999
0.111
.111
0
It will block things like:
1,1,1.111
000,001.111
999.
0.
111.110000
1.1.1.111
9.909,888
There are several ways to make this regex simpler and shorter, but understand that changing the pattern will loosen what it considers a number.
Since many regex engines (e.g. JavaScript and Ruby) don't support the negative lookbehind, the only way to do this correctly is with capture groups:
(?:^|\s)(?=.)((?:0|(?:[1-9](?:\d*|\d{0,2}(?:,\d{3})*)))?(?:\.\d*[1-9])?)(?!\S)
The numbers you're looking for will be in capture group 1.
Tested here: http://rubular.com/r/3HCSkndzhT
Obviously, this is a massive, complicated, nigh-unreadable regex. I enjoyed the challenge, but you should consider whether you really want to use this in a production environment. Instead of trying to do everything in one step, you could do it in two: a regex to catch anything that might be a number, then another one to weed out whatever isn't a number. Or you could do some basic processing, then use your language's built-in number parsing functions. Your choice.
The regex below will match both numbers from your example.
\b\d[\d,.]*\b
It will return 5000 and 99,999.99998713 - matching your requirements.
Some days ago, I worked on the problem of removing trailing zeros from the string of a number.
In the continuity of that problem, I find this one interesting because it widens the problem to numbers comprising commas.
I have taken the regex's pattern I had writen in that previous problem I worked on and I improved it in order that it can treat the numbers with commas as an answer for this problem.
I've been carried away with my enthusiasm and my liking of regexes. I don't know if the result fits exactly to the need expressed by Michael Prescott. I would be interested to know the points that are in excess or in lack in my regex, and to correct it to make it more suitable for you.
Now, after a long session of work on this regex, I have a sort of weight in the brain, so I'm not fresh enough to give a lot of explanation. If points are obscure, and if anybody may come to be interested enough, please, ask me.
The regex is built in order that it can detect the numbers expressed in scientific notation 2E10 or even 5,22,454.12E-00.0478 , removing unnecessary zeros in the two parts of such numbers too. If an exponent is equal to zero , the number is modified so that there is no more exponent.
I put some verification in the pattern so that some particular cases will not match, for exemple '12..57' won't match. But in ',111' the string '111' matches because the preceding comma is considered a comma not being in a number but a comma of sentence.
I think that the managing of commas should be improved, because it seems to me that there are only 2 digits between commas in Indian numbering. It won't be dificult to correct, I presume
Here after is a code demonstrating how my regex works. There are two functions, according if one wants the numbers '.1245' to be transformed in '0.1245' or not. I wouldn't be surprised if errors or unwanted matchings or unmatchings will remain for certain cases of number strings; then I'd like to know these cases to understand and correct the deficiency.
I apologize for this code written in Python, but regexes are trans-langage and I think everybody will be capable of undertsanding the reex's pattern
import re
regx = re.compile('(?<![\d.])(?!\.\.)(?<![\d.][eE][+-])(?<![\d.][eE])(?<!\d[.,])'
'' #---------------------------------
'([+-]?)'
'(?![\d,]*?\.[\d,]*?\.[\d,]*?)'
'(?:0|,(?=0)|(?<!\d),)*'
'(?:'
'((?:\d(?!\.[1-9])|,(?=\d))+)[.,]?'
'|\.(0)'
'|((?<!\.)\.\d+?)'
'|([\d,]+\.\d+?))'
'0*'
'' #---------------------------------
'(?:'
'([eE][+-]?)(?:0|,(?=0))*'
'(?:'
'(?!0+(?=\D|\Z))((?:\d(?!\.[1-9])|,(?=\d))+)[.,]?'
'|((?<!\.)\.(?!0+(?=\D|\Z))\d+?)'
'|([\d,]+\.(?!0+(?=\D|\Z))\d+?))'
'0*'
')?'
'' #---------------------------------
'(?![.,]?\d)')
def dzs_numbs(x,regx = regx): # ds = detect and zeros-shave
if not regx.findall(x):
yield ('No match,', 'No catched string,', 'No groups.')
for mat in regx.finditer(x):
yield (mat.group(), ''.join(mat.groups('')), mat.groups(''))
def dzs_numbs2(x,regx = regx): # ds = detect and zeros-shave
if not regx.findall(x):
yield ('No match,', 'No catched string,', 'No groups.')
for mat in regx.finditer(x):
yield (mat.group(),
''.join(('0' if n.startswith('.') else '')+n for n in mat.groups('')),
mat.groups(''))
NS = [' 23456000and23456000. or23456000.000 00023456000 s000023456000. 000023456000.000 ',
'arf 10000 sea10000.+10000.000 00010000-00010000. kant00010000.000 ',
' 24: 24, 24. 24.000 24.000, 00024r 00024. blue 00024.000 ',
' 8zoom8. 8.000 0008 0008. and0008.000 ',
' 0 00000M0. = 000. 0.0 0.000 000.0 000.000 .000000 .0 ',
' .0000023456 .0000023456000 '
' .0005872 .0005872000 .00503 .00503000 ',
' .068 .0680000 .8 .8000 .123456123456 .123456123456000 ',
' .657 .657000 .45 .4500000 .7 .70000 0.0000023230000 000.0000023230000 ',
' 0.0081000 0000.0081000 0.059000 0000.059000 ',
' 0.78987400000 snow 00000.78987400000 0.4400000 00000.4400000 ',
' -0.5000 -0000.5000 0.90 000.90 0.7 000.7 ',
' 2.6 00002.6 00002.60000 4.71 0004.71 0004.7100 ',
' 23.49 00023.49 00023.490000 103.45 0000103.45 0000103.45000 ',
' 10003.45067 000010003.45067 000010003.4506700 ',
' +15000.0012 +000015000.0012 +000015000.0012000 ',
' 78000.89 000078000.89 000078000.89000 ',
' .0457e10 .0457000e10 00000.0457000e10 ',
' 258e8 2580000e4 0000000002580000e4 ',
' 0.782e10 0000.782e10 0000.7820000e10 ',
' 1.23E2 0001.23E2 0001.2300000E2 ',
' 432e-102 0000432e-102 004320000e-106 ',
' 1.46e10and0001.46e10 0001.4600000e10 ',
' 1.077e-300 0001.077e-300 0001.077000e-300 ',
' 1.069e10 0001.069e10 0001.069000e10 ',
' 105040.03e10 000105040.03e10 105040.0300e10 ',
' +286E000024.487900 -78.4500e.14500 .0140E789. ',
' 081,12.40E07,95.0120 0045,78,123.03500e-0.00 ',
' 0096,78,473.0380e-0. 0008,78,373.066000E0. 0004512300.E0000 ',
' ..18000 25..00 36...77 2..8 ',
' 3.8..9 .12500. 12.51.400 ',
' 00099,111.8713000 -0012,45,83,987.26+0.000,099,88,44.or00,00,00.00must',
' 00099,44,and 0000,099,88,44.bom',
'00,000,00.587000 77,98,23,45., this,that ',
' ,111 145.20 +9,9,9 0012800 .,,. 1 100,000 ',
'1,1,1.111 000,001.111 -999. 0. 111.110000 1.1.1.111 9.909,888']
for ch in NS:
print 'string: '+repr(ch)
for strmatch, modified, the_groups in dzs_numbs2(ch):
print strmatch.rjust(20),'',modified,'',the_groups
print
result
string: ' 23456000and23456000. or23456000.000 00023456000 s000023456000. 000023456000.000 '
23456000 23456000 ('', '23456000', '', '', '', '', '', '', '')
23456000. 23456000 ('', '23456000', '', '', '', '', '', '', '')
23456000.000 23456000 ('', '23456000', '', '', '', '', '', '', '')
00023456000 23456000 ('', '23456000', '', '', '', '', '', '', '')
000023456000. 23456000 ('', '23456000', '', '', '', '', '', '', '')
000023456000.000 23456000 ('', '23456000', '', '', '', '', '', '', '')
string: 'arf 10000 sea10000.+10000.000 00010000-00010000. kant00010000.000 '
10000 10000 ('', '10000', '', '', '', '', '', '', '')
10000. 10000 ('', '10000', '', '', '', '', '', '', '')
10000.000 10000 ('', '10000', '', '', '', '', '', '', '')
00010000 10000 ('', '10000', '', '', '', '', '', '', '')
00010000. 10000 ('', '10000', '', '', '', '', '', '', '')
00010000.000 10000 ('', '10000', '', '', '', '', '', '', '')
string: ' 24: 24, 24. 24.000 24.000, 00024r 00024. blue 00024.000 '
24 24 ('', '24', '', '', '', '', '', '', '')
24, 24 ('', '24', '', '', '', '', '', '', '')
24. 24 ('', '24', '', '', '', '', '', '', '')
24.000 24 ('', '24', '', '', '', '', '', '', '')
24.000 24 ('', '24', '', '', '', '', '', '', '')
00024 24 ('', '24', '', '', '', '', '', '', '')
00024. 24 ('', '24', '', '', '', '', '', '', '')
00024.000 24 ('', '24', '', '', '', '', '', '', '')
string: ' 8zoom8. 8.000 0008 0008. and0008.000 '
8 8 ('', '8', '', '', '', '', '', '', '')
8. 8 ('', '8', '', '', '', '', '', '', '')
8.000 8 ('', '8', '', '', '', '', '', '', '')
0008 8 ('', '8', '', '', '', '', '', '', '')
0008. 8 ('', '8', '', '', '', '', '', '', '')
0008.000 8 ('', '8', '', '', '', '', '', '', '')
string: ' 0 00000M0. = 000. 0.0 0.000 000.0 000.000 .000000 .0 '
0 0 ('', '0', '', '', '', '', '', '', '')
00000 0 ('', '0', '', '', '', '', '', '', '')
0. 0 ('', '0', '', '', '', '', '', '', '')
000. 0 ('', '0', '', '', '', '', '', '', '')
0.0 0 ('', '', '0', '', '', '', '', '', '')
0.000 0 ('', '', '0', '', '', '', '', '', '')
000.0 0 ('', '', '0', '', '', '', '', '', '')
000.000 0 ('', '', '0', '', '', '', '', '', '')
.000000 0 ('', '', '0', '', '', '', '', '', '')
.0 0 ('', '', '0', '', '', '', '', '', '')
string: ' .0000023456 .0000023456000 .0005872 .0005872000 .00503 .00503000 '
.0000023456 0.0000023456 ('', '', '', '.0000023456', '', '', '', '', '')
.0000023456000 0.0000023456 ('', '', '', '.0000023456', '', '', '', '', '')
.0005872 0.0005872 ('', '', '', '.0005872', '', '', '', '', '')
.0005872000 0.0005872 ('', '', '', '.0005872', '', '', '', '', '')
.00503 0.00503 ('', '', '', '.00503', '', '', '', '', '')
.00503000 0.00503 ('', '', '', '.00503', '', '', '', '', '')
string: ' .068 .0680000 .8 .8000 .123456123456 .123456123456000 '
.068 0.068 ('', '', '', '.068', '', '', '', '', '')
.0680000 0.068 ('', '', '', '.068', '', '', '', '', '')
.8 0.8 ('', '', '', '.8', '', '', '', '', '')
.8000 0.8 ('', '', '', '.8', '', '', '', '', '')
.123456123456 0.123456123456 ('', '', '', '.123456123456', '', '', '', '', '')
.123456123456000 0.123456123456 ('', '', '', '.123456123456', '', '', '', '', '')
string: ' .657 .657000 .45 .4500000 .7 .70000 0.0000023230000 000.0000023230000 '
.657 0.657 ('', '', '', '.657', '', '', '', '', '')
.657000 0.657 ('', '', '', '.657', '', '', '', '', '')
.45 0.45 ('', '', '', '.45', '', '', '', '', '')
.4500000 0.45 ('', '', '', '.45', '', '', '', '', '')
.7 0.7 ('', '', '', '.7', '', '', '', '', '')
.70000 0.7 ('', '', '', '.7', '', '', '', '', '')
0.0000023230000 0.000002323 ('', '', '', '.000002323', '', '', '', '', '')
000.0000023230000 0.000002323 ('', '', '', '.000002323', '', '', '', '', '')
string: ' 0.0081000 0000.0081000 0.059000 0000.059000 '
0.0081000 0.0081 ('', '', '', '.0081', '', '', '', '', '')
0000.0081000 0.0081 ('', '', '', '.0081', '', '', '', '', '')
0.059000 0.059 ('', '', '', '.059', '', '', '', '', '')
0000.059000 0.059 ('', '', '', '.059', '', '', '', '', '')
string: ' 0.78987400000 snow 00000.78987400000 0.4400000 00000.4400000 '
0.78987400000 0.789874 ('', '', '', '.789874', '', '', '', '', '')
00000.78987400000 0.789874 ('', '', '', '.789874', '', '', '', '', '')
0.4400000 0.44 ('', '', '', '.44', '', '', '', '', '')
00000.4400000 0.44 ('', '', '', '.44', '', '', '', '', '')
string: ' -0.5000 -0000.5000 0.90 000.90 0.7 000.7 '
-0.5000 -0.5 ('-', '', '', '.5', '', '', '', '', '')
-0000.5000 -0.5 ('-', '', '', '.5', '', '', '', '', '')
0.90 0.9 ('', '', '', '.9', '', '', '', '', '')
000.90 0.9 ('', '', '', '.9', '', '', '', '', '')
0.7 0.7 ('', '', '', '.7', '', '', '', '', '')
000.7 0.7 ('', '', '', '.7', '', '', '', '', '')
string: ' 2.6 00002.6 00002.60000 4.71 0004.71 0004.7100 '
2.6 2.6 ('', '', '', '', '2.6', '', '', '', '')
00002.6 2.6 ('', '', '', '', '2.6', '', '', '', '')
00002.60000 2.6 ('', '', '', '', '2.6', '', '', '', '')
4.71 4.71 ('', '', '', '', '4.71', '', '', '', '')
0004.71 4.71 ('', '', '', '', '4.71', '', '', '', '')
0004.7100 4.71 ('', '', '', '', '4.71', '', '', '', '')
string: ' 23.49 00023.49 00023.490000 103.45 0000103.45 0000103.45000 '
23.49 23.49 ('', '', '', '', '23.49', '', '', '', '')
00023.49 23.49 ('', '', '', '', '23.49', '', '', '', '')
00023.490000 23.49 ('', '', '', '', '23.49', '', '', '', '')
103.45 103.45 ('', '', '', '', '103.45', '', '', '', '')
0000103.45 103.45 ('', '', '', '', '103.45', '', '', '', '')
0000103.45000 103.45 ('', '', '', '', '103.45', '', '', '', '')
string: ' 10003.45067 000010003.45067 000010003.4506700 '
10003.45067 10003.45067 ('', '', '', '', '10003.45067', '', '', '', '')
000010003.45067 10003.45067 ('', '', '', '', '10003.45067', '', '', '', '')
000010003.4506700 10003.45067 ('', '', '', '', '10003.45067', '', '', '', '')
string: ' +15000.0012 +000015000.0012 +000015000.0012000 '
+15000.0012 +15000.0012 ('+', '', '', '', '15000.0012', '', '', '', '')
+000015000.0012 +15000.0012 ('+', '', '', '', '15000.0012', '', '', '', '')
+000015000.0012000 +15000.0012 ('+', '', '', '', '15000.0012', '', '', '', '')
string: ' 78000.89 000078000.89 000078000.89000 '
78000.89 78000.89 ('', '', '', '', '78000.89', '', '', '', '')
000078000.89 78000.89 ('', '', '', '', '78000.89', '', '', '', '')
000078000.89000 78000.89 ('', '', '', '', '78000.89', '', '', '', '')
string: ' .0457e10 .0457000e10 00000.0457000e10 '
.0457e10 0.0457e10 ('', '', '', '.0457', '', 'e', '10', '', '')
.0457000e10 0.0457e10 ('', '', '', '.0457', '', 'e', '10', '', '')
00000.0457000e10 0.0457e10 ('', '', '', '.0457', '', 'e', '10', '', '')
string: ' 258e8 2580000e4 0000000002580000e4 '
258e8 258e8 ('', '258', '', '', '', 'e', '8', '', '')
2580000e4 2580000e4 ('', '2580000', '', '', '', 'e', '4', '', '')
0000000002580000e4 2580000e4 ('', '2580000', '', '', '', 'e', '4', '', '')
string: ' 0.782e10 0000.782e10 0000.7820000e10 '
0.782e10 0.782e10 ('', '', '', '.782', '', 'e', '10', '', '')
0000.782e10 0.782e10 ('', '', '', '.782', '', 'e', '10', '', '')
0000.7820000e10 0.782e10 ('', '', '', '.782', '', 'e', '10', '', '')
string: ' 1.23E2 0001.23E2 0001.2300000E2 '
1.23E2 1.23E2 ('', '', '', '', '1.23', 'E', '2', '', '')
0001.23E2 1.23E2 ('', '', '', '', '1.23', 'E', '2', '', '')
0001.2300000E2 1.23E2 ('', '', '', '', '1.23', 'E', '2', '', '')
string: ' 432e-102 0000432e-102 004320000e-106 '
432e-102 432e-102 ('', '432', '', '', '', 'e-', '102', '', '')
0000432e-102 432e-102 ('', '432', '', '', '', 'e-', '102', '', '')
004320000e-106 4320000e-106 ('', '4320000', '', '', '', 'e-', '106', '', '')
string: ' 1.46e10and0001.46e10 0001.4600000e10 '
1.46e10 1.46e10 ('', '', '', '', '1.46', 'e', '10', '', '')
0001.46e10 1.46e10 ('', '', '', '', '1.46', 'e', '10', '', '')
0001.4600000e10 1.46e10 ('', '', '', '', '1.46', 'e', '10', '', '')
string: ' 1.077e-300 0001.077e-300 0001.077000e-300 '
1.077e-300 1.077e-300 ('', '', '', '', '1.077', 'e-', '300', '', '')
0001.077e-300 1.077e-300 ('', '', '', '', '1.077', 'e-', '300', '', '')
0001.077000e-300 1.077e-300 ('', '', '', '', '1.077', 'e-', '300', '', '')
string: ' 1.069e10 0001.069e10 0001.069000e10 '
1.069e10 1.069e10 ('', '', '', '', '1.069', 'e', '10', '', '')
0001.069e10 1.069e10 ('', '', '', '', '1.069', 'e', '10', '', '')
0001.069000e10 1.069e10 ('', '', '', '', '1.069', 'e', '10', '', '')
string: ' 105040.03e10 000105040.03e10 105040.0300e10 '
105040.03e10 105040.03e10 ('', '', '', '', '105040.03', 'e', '10', '', '')
000105040.03e10 105040.03e10 ('', '', '', '', '105040.03', 'e', '10', '', '')
105040.0300e10 105040.03e10 ('', '', '', '', '105040.03', 'e', '10', '', '')
string: ' +286E000024.487900 -78.4500e.14500 .0140E789. '
+286E000024.487900 +286E24.4879 ('+', '286', '', '', '', 'E', '', '', '24.4879')
-78.4500e.14500 -78.45e0.145 ('-', '', '', '', '78.45', 'e', '', '.145', '')
.0140E789. 0.014E789 ('', '', '', '.014', '', 'E', '789', '', '')
string: ' 081,12.40E07,95.0120 0045,78,123.03500e-0.00 '
081,12.40E07,95.0120 81,12.4E7,95.012 ('', '', '', '', '81,12.4', 'E', '', '', '7,95.012')
0045,78,123.03500 45,78,123.035 ('', '', '', '', '45,78,123.035', '', '', '', '')
string: ' 0096,78,473.0380e-0. 0008,78,373.066000E0. 0004512300.E0000 '
0096,78,473.0380 96,78,473.038 ('', '', '', '', '96,78,473.038', '', '', '', '')
0008,78,373.066000 8,78,373.066 ('', '', '', '', '8,78,373.066', '', '', '', '')
0004512300. 4512300 ('', '4512300', '', '', '', '', '', '', '')
string: ' ..18000 25..00 36...77 2..8 '
No match, No catched string, No groups.
string: ' 3.8..9 .12500. 12.51.400 '
No match, No catched string, No groups.
string: ' 00099,111.8713000 -0012,45,83,987.26+0.000,099,88,44.or00,00,00.00must'
00099,111.8713000 99,111.8713 ('', '', '', '', '99,111.8713', '', '', '', '')
-0012,45,83,987.26 -12,45,83,987.26 ('-', '', '', '', '12,45,83,987.26', '', '', '', '')
00,00,00.00 0 ('', '', '0', '', '', '', '', '', '')
string: ' 00099,44,and 0000,099,88,44.bom'
00099,44, 99,44 ('', '99,44', '', '', '', '', '', '', '')
0000,099,88,44. 99,88,44 ('', '99,88,44', '', '', '', '', '', '', '')
string: '00,000,00.587000 77,98,23,45., this,that '
00,000,00.587000 0.587 ('', '', '', '.587', '', '', '', '', '')
77,98,23,45. 77,98,23,45 ('', '77,98,23,45', '', '', '', '', '', '', '')
string: ' ,111 145.20 +9,9,9 0012800 .,,. 1 100,000 '
,111 111 ('', '111', '', '', '', '', '', '', '')
145.20 145.2 ('', '', '', '', '145.2', '', '', '', '')
+9,9,9 +9,9,9 ('+', '9,9,9', '', '', '', '', '', '', '')
0012800 12800 ('', '12800', '', '', '', '', '', '', '')
1 1 ('', '1', '', '', '', '', '', '', '')
100,000 100,000 ('', '100,000', '', '', '', '', '', '', '')
string: '1,1,1.111 000,001.111 -999. 0. 111.110000 1.1.1.111 9.909,888'
1,1,1.111 1,1,1.111 ('', '', '', '', '1,1,1.111', '', '', '', '')
000,001.111 1.111 ('', '', '', '', '1.111', '', '', '', '')
-999. -999 ('-', '999', '', '', '', '', '', '', '')
0. 0 ('', '0', '', '', '', '', '', '', '')
111.110000 111.11 ('', '', '', '', '111.11', '', '', '', '')
\d+(,\d+)*(\.\d+)?
This assumes that there is always at least one digit before or after any comma or decimal and also assumes that there is at most one decimal and that all the commas precede the decimal.
Taking a certain liberty with the requirements, you're looking for
\d+([\d,]?\d)*(\.\d+)?
But notice this will match e.g. 11,11,1
This regex:
(\d{1,3},\d{3}(,\d{3})*)(\.\d*)?|\d+\.?\d*
Matched every number in the string:
1 1.0 0.1 1.001 1,000 1,000,000 1000.1 1,000.1 1,323,444,000 1,999 1,222,455,666.0 1,244
Here's a regex:
(?:\d+)((\d{1,3})*([\,\ ]\d{3})*)(\.\d+)?
that accepts numbers:
123456789
, 123.123
123 456 789
, 123 456 789.100
, 123,456
, 3,232,300,000.00
Tests: http://regexr.com/3h1a2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With