2017-01-25 33 views
0
import re 
p2b = open('repattern2b.txt').read().rstrip() 

我需要編寫一個正則表達式模式,它匹配表示以科學記數法編寫的數字的字符串。但除此之外,確保組1是尾數的符號(如果有符號);組2是尾數,但只有它不是0(這種例外使得模式更簡單);第3組是指數。如何修復正則表達式

例如:如果

m = re.match(the pattern,’9.11x10^-31’) 

然後m.groups()是

(None, '9.11', '-31'). 

不應該有更多的組。

下面是正則表達式我寫了「repattern2b.txt」:

^([+-]?)([1-9].[0-9]+)x10^([1-9][0-9]*)$ 

但我得到的錯誤:

54 *Error: re.match(p2b,'0').groups() raised exception; unevaluated: (None, None, None) 
55 *Error: re.match(p2b,'5').groups() raised exception; unevaluated: (None, '5', None) 
56 *Error: re.match(p2b,'5.0').groups() raised exception; unevaluated: (None, '5.0', None) 
57 *Error: re.match(p2b,'5.2x10^31').groups() raised exception; unevaluated: (None, '5.2', '31') 
58 *Error: re.match(p2b,'5.2x10^-31').groups() raised exception; unevaluated: (None, '5.2', '-31') 
59 *Error: re.match(p2b,'5.2x10^+31').groups() raised exception; unevaluated: (None, '5.2', '+31') 
60 *Error: re.match(p2b,'-5.2x10^-31').groups() raised exception; unevaluated: ('-', '5.2', '-31') 

看來,我的正則表達式引發了異常,但我我不知道爲什麼。有人可以幫我解決它嗎?提前致謝。

+0

通常regexes本身不會引發異常。你確定你餵它一串嗎? –

+0

是的,我認爲我做了,它可能是導致問題的分組,但我不確定 – zhangdi

回答

0

例外是因爲發生您的re.match返回None。然後您無法訪問None.groups()

爲什麼它返回None的一切? 您在表達式的中間有一個^,並且在正則表達式中表示行的開始。例如,您可以在表達式的開始處使用它。

比較:

>>> re.match(r"^([+-]?)([1-9].[0-9]+)x10^([1-9][0-9]*)$",'5.2x10^31') 
None 

有:

>>> re.match(r"^([+-]?)([1-9].[0-9]+)x10\^([1-9][0-9]*)$",'5.2x10^31') 
<_sre.SRE_Match object; span=(0, 9), match='5.2x10^31'> 
+0

嗨,改變後,只有re.match(p2b,'5.2x10^31')。groups()不會引發一個例外,但其他案件仍在引發例外。 – zhangdi

+0

是的......但是你的表情有各種各樣的其他問題...... – donkopotamus

0

與測試數據的正則表達式相比,有幾個問題:

  • 指數的加/減不在正則表達式中
  • ^ in字符串電子中間沒有逃過
  • 10^...可能不存在中的數據,但它是在正則表達式
  • 第一.可能不存在中的數據,但它是在正則表達式
  • 第一加/減後的問號必須在組之外,如果你想有一個None當符號缺失

也許這個作品:

import re 

p2b = '^([+-])?(([1-9].?[0-9]*)|0)(x10\^([+-]?[1-9][0-9]*))?$' 

for s in ['-5.2', '+1.2', '0', '5.', '5.0', 
      '5.2x10^31', '5.2x10^-31', '5.2x10^+31', '-5.2x10^-31']: 
    try: 
     a = re.match(p2b, s).groups() 
     a = (a[0], a[2], a[4]) 
     print s, ": ", a 
    except Exception as e: 
     print s, ": ", e 

這裏有一些解釋:

p2b = re.compile(r""" 
     ^      # start of line 
     ([+-])?     # maybe a sign 
     (
      (
       [1-9].?[0-9]* # accept 1, 2, 5., 5.2, not 0 
      ) | 0    # 0 will not be in a group 
     ) 
     ( 
      x10\^    # the x10... will be skipped later 
       (
       [+-]?   # exponent might have a sign 
       [1-9][0-9]* # one or more digits, not starting with 0 
      ) 
     )?      # The x10... might be missing 
     $      # end of line 
     """, re.VERBOSE) 

這是輸出:

-5.2 : ('-', '5.2', None) 
+1.2 : ('+', '1.2', None) 
0 : (None, None, None) 
5. : (None, '5.', None) 
5.0 : (None, '5.0', None) 
5 : (None, '5', None) 
15 : (None, '15', None) 
5.2x10^31 : (None, '5.2', '31') 
5.2x10^-31 : (None, '5.2', '-31') 
5.2x10^+31 : (None, '5.2', '+31') 
-5.2x10^-31 : ('-', '5.2', '-31') 

a[2]將包含'x10^-31',所以我跳過它,一定會有更好的解決方案。

+0

嘿,你的代碼很棒,但只有一個問題。 0:應該是(None,None,None)而不是(None,'0',None) – zhangdi