, expression.transformString(), unsested [] . transformString scanString.
, [] , , "" , :
src = """[This] is some text with [some [blocks that are
nested [in a [variety] of ways]] in various places]"""
, - . - , , "" " " . nestedExpr. . :
from pyparsing import nestedExpr, ParseResults, CharsNotIn
out = []
last = 0
for tokens,start,end in nestedExpr("[","]").scanString(src):
out.append(src[last:start])
if not any(isinstance(tok,ParseResults) for tok in tokens[0]):
out.append(src[start:end])
last = end
out.append(src[last:])
print "".join(out)
out = []
last = 0
for t,s,e in nestedExpr("[","]").scanString(src):
out.append(src[last:s])
topLevel = [tok for tok in t[0] if not isinstance(tok,ParseResults)]
out.append('['+" ".join(topLevel)+']')
last = e
out.append(src[last:])
print "".join(out)
out = []
last = 0
for t,s,e in nestedExpr("[","]", CharsNotIn('[]')).scanString(src):
out.append(src[last:s])
for tok in t[0]:
if isinstance(tok,ParseResults): continue
out.append('['+tok.strip()+']')
last = e
out.append(src[last:])
print "".join(out)
:
[This] is some text with
[This] is some text with [some in various places]
[This] is some text with [some][in various places]
, . , nestedExpr.