1

PyParsing を使用して先頭または末尾の空白文字を削除して、コードをきれいにしようとしています。FollowedBy文字列に一致するがそれを含まないサブクラスを利用できるため、先頭の空白を削除するのは非常に簡単でした。これで、識別文字列に続くものにも同じものが必要になります。

ここに小さな例があります:

from pyparsing import *

insource = """
annotation (Documentation(info="  
  <html>  
<b>FOO</b>
</html>  
 "));
"""
# Working replacement:
HTMLStartref = OneOrMore(White(' \t\n')) + (FollowedBy(CaselessLiteral('<html>')))

## Not working because of non-existing "LeadBy" 
# HTMLEndref = LeadBy(CaselessLiteral('</html>')) + OneOrMore(White(' \t\n')) + FollowedBy('"')

out = Suppress(HTMLStartref).transformString(insource)
out2 = Suppress(HTMLEndref).transformString(out)

出力として次のようになります。

>>> print out
annotation (Documentation(info="<html>
<b>FOO</b>
</html>
 "));

そして取得する必要があります:

>>> print out2
annotation (Documentation(info="<html>
<b>FOO</b>
</html>"));

私はドキュメントを見ましたが、" LeadBy" に相当するものFollowedByやそれを達成する方法を見つけることができませんでした。

4

1 に答える 1

2

あなたが求めているのは、「後読み」のようなものです。つまり、特定のパターンが先行する場合にのみ一致します。現時点では、そのための明示的なクラスは実際にはありませんが、やりたいことのために、左から右に変換し、先頭部分をそのままにして、それを抑制せず、空白を抑制することができます.

問題に対処するには、次の 2 つの方法があります。

# define expressions to match leading and trailing
# html tags, and just suppress the leading or trailing whitespace
opener = White().suppress() + Literal("<html>")
closer = Literal("</html>") + White().suppress()

# define a single expression to match either opener
# or closer - have to add leaveWhitespace() call so that
# we catch the leading whitespace in opener
either = opener|closer
either.leaveWhitespace()

print either.transformString(insource) 


# alternative, if you know what the tag will look like:
# match 'info=<some double quoted string>', and use a parse
# action to extract the contents within the quoted string,
# call strip() to remove leading and trailing whitespace,
# and then restore the original '"' characters (which are
# auto-stripped by the QuotedString class by default)
infovalue = QuotedString('"', multiline=True)
infovalue.setParseAction(lambda t: '"' + t[0].strip() + '"')
infoattr = "info=" + infovalue

print infoattr.transformString(insource)
于 2012-08-02T12:41:19.403 に答える