2014-01-07 45 views
2

我試圖用pyparsing匹配一個多行字符串,可以以類似的方式繼續與蟒蛇:的Python/Pyparsing - 多行報價

Test = "This is a long " \ 
     "string" 

我不能找到一種方法,使pyparsing承認這一點。以下是我試過到目前爲止:

import pyparsing as pp 

src1 = ''' 
Test("This is a long string") 
''' 

src2 = ''' 
Test("This is a long " \ 
    "string") 
''' 

_lp = pp.Suppress('(') 
_rp = pp.Suppress(')') 
_str = pp.QuotedString('"', multiline=True, unquoteResults=False) 
func = pp.Word(pp.alphas) 

function = func + _lp + _str + _rp 
print src1 
print function.parseString(src1) 
print '-------------------------' 
print src2 
print function.parseString(src2) 

回答

4

的問題是,具有多行帶引號的字符串不做你的想法。多行帶引號的字符串是字面上 - 用換行字符串中:

import pyparsing as pp 

src0 = ''' 
"Hello 
World 
Goodbye and go" 
''' 

pat = pp.QuotedString('"', multiline=True) 
print pat.parseString(src0) 

解析這個字符串的輸出是['Hello\n World\n Goodbye and go']

據我所知,如果你想要一個字符串,它類似於Python的字符串的行爲,你必須自己定義它:

import pyparsing as pp 

src1 = ''' 
Test("This is a long string") 
''' 

src2 = ''' 
Test("This is a long" 
    "string") 
''' 

src3 = ''' 

Test("This is a long" \\ 
    "string") 
''' 

_lp = pp.Suppress('(') 
_rp = pp.Suppress(')') 
_str = pp.QuotedString('"') 
_slash = pp.Suppress(pp.Optional("\\")) 
_multiline_str = pp.Combine(pp.OneOrMore(_str + _slash), adjacent=False) 

func = pp.Word(pp.alphas) 

function = func + _lp + _multiline_str + _rp 

print src1 
print function.parseString(src1) 
print '-------------------------' 
print src2 
print function.parseString(src2) 
print '-------------------------' 
print src3 
print function.parseString(src3) 

這將產生以下的輸出:

Test("This is a long string") 

['Test', 'This is a long string'] 
------------------------- 

Test("This is a long" 
    "string") 

['Test', 'This is a longstring'] 
------------------------- 

Test("This is a long" \ 
    "string") 

['Test', 'This is a longstring'] 

注意:Combine類將各種引用的字符串合併爲一個單元,以使它們在輸出列表中顯示爲單個字符串。反斜槓被壓縮的原因是它沒有被組合成輸出字符串的一部分。

+0

謝謝,這是我所希望的! –