在PyParsing中如何构造与followdby子类等价的“leading”

2024-10-06 19:29:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图通过使用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)

作为输出:

^{pr2}$

并且应该得到:

^{3}$

我查看了documentation,但找不到与FollowedBy等价的“LeadBy”或如何实现这一点的方法。在


Tags: 方法字符串htmloutwhite前导suppressoneormore
1条回答
网友
1楼 · 发布于 2024-10-06 19:29:28

你要的是类似“lookback”的东西,也就是说,只有在某个特定的模式前面有一个匹配。我现在还没有一个显式的类,但是对于你想要做的,你仍然可以从左到右的转换,只保留在前导部分,而不是抑制它,只抑制空白。在

以下是几种解决问题的方法:

# 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)

相关问题 更多 >