我试图通过使用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
”或如何实现这一点的方法。在
你要的是类似“lookback”的东西,也就是说,只有在某个特定的模式前面有一个匹配。我现在还没有一个显式的类,但是对于你想要做的,你仍然可以从左到右的转换,只保留在前导部分,而不是抑制它,只抑制空白。在
以下是几种解决问题的方法:
相关问题 更多 >
编程相关推荐