며칠 전에 저는 숫자 문자열에서 후행 0을 제거하는 문제를 해결 했습니다. .
그 문제의 연속성에서 나는 이것이 쉼표로 구성된 숫자로 문제를 넓히기 때문에 흥미로운 것을 발견했습니다.
이전에 작업했던 문제에서 작성한 정규식 패턴을 가져와이 문제에 대한 답으로 쉼표로 숫자를 처리 할 수 있도록 개선했습니다.
나는 나의 열정과 내가 좋아하는 정규식에 푹 빠졌다. 결과가 Michael Prescott이 표현한 요구에 정확히 맞는지 모르겠습니다. 내 정규식에서 과도하거나 부족한 점을 알고 더 적합하도록 수정하는 데 관심이 있습니다.
자,이 정규식에 대한 긴 세션 후에, 저는 뇌에 일종의 무게가있어서 많은 설명을 할만큼 신선하지 않습니다. 포인트가 모호하고 관심이 있으신 분이 계시다면 제게 물어보세요.
정규식은 과학적 표기법 2E10 또는 5,22,454.12E-00.0478로 표현 된 숫자를 감지 할 수 있도록 작성되었습니다. 이러한 숫자의 두 부분에서도 불필요한 0을 제거합니다. 지수가 0과 같으면 더 이상 지수가 없도록 숫자가 수정됩니다.
예를 들어 '12 ..57 ' 이 일치하지 않기 때문에 일부 특정 사례가 일치하지 않도록 패턴에 몇 가지 확인을 넣었 습니다. 그러나 ', 111' 에서 문자열 '111' 은 앞의 쉼표가 숫자가 아니라 쉼표의 문장으로 간주되기 때문에 일치합니다.
인도 넘버링에서는 쉼표 사이에 2 자리 만있는 것 같아서 쉼표 관리를 개선해야한다고 생각합니다. 수정하기 어렵지 않을 것 같아요
다음은 내 정규식이 어떻게 작동하는지 보여주는 코드입니다. 이 개 기능은 하나의 숫자를 원하는 경우에 따라이있다 '0.1245이' 에서 변형 될 '0.1245' 여부. 숫자 문자열의 특정 경우에 오류나 원치 않는 일치 또는 불일치가 남아 있어도 놀라지 않을 것입니다. 그런 다음 결함을 이해하고 수정하기 위해 이러한 사례를 알고 싶습니다.
파이썬으로 작성된이 코드에 대해 사과드립니다. 그러나 정규식은 언어를 초월하며 모두가 reex의 패턴을 이해할 수있을 것이라고 생각합니다.
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
결과
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', '', '', '', '')