【发布时间】:2015-12-02 17:00:30
【问题描述】:
目的是将C++ boost 中的这些正则表达式转换为Python re 正则表达式:
typedef boost::u32regex tRegex;
tRegex emptyre = boost::make_u32regex("^$");
tRegex commentre = boost::make_u32regex("^;.*$");
tRegex versionre = boost::make_u32regex("^@\\$Date: (.*) \\$$");
tRegex includere = boost::make_u32regex("^<(\\S+)$");
tRegex rungroupre = boost::make_u32regex("^>(\\d+)$");
tRegex readreppre = boost::make_u32regex("^>(\\S+)$");
tRegex tokre = boost::make_u32regex("^:(.*)$");
tRegex groupstartre = boost::make_u32regex("^#(\\d+)$");
tRegex groupendre = boost::make_u32regex("^#$");
tRegex rulere = boost::make_u32regex("^([!-+^])([^\\t]+)\\t+([^\\t]*)$");
我可以一一重写这些正则表达式,但是上面的例子还有很多,所以我的问题是关于
- 如何将 C++ boost 正则表达式转换为 Python 和
- boost regex 和 python re regex 有什么区别?
C++ boost::u32regex 是否与 python 中的 re 正则表达式相同? 如果不是,有什么区别? (文档的链接将不胜感激=))例如:
- 在boost中,有
boost::u32regex_match,是一样的吗re.match? - boost里面有
boost::u32regex_search,和re.search有什么区别 - 还有
boost::format_perl和boost::match_default和boost::smatch,它们在pythonre中的等价物是什么?
【问题讨论】:
-
请注意
[!-+]匹配!,",#,$,%,&,',(, @87654348@, @,*和+(rulere正则表达式)。这里的所有正则表达式在 Python 中的工作方式都是一样的。如果您有更复杂的模式,请分享。re.match与regex_match不同,因为re.match在字符串开头寻找匹配,而regex_match需要完整的字符串匹配。regex_search的行为与regex_search相同。您可以将$锚添加到与regex_match一起使用的模式中,并将它们传递给re.match。没有boost::format_perl和boost::match_default标志。匹配是finditer。 -
其实就是从这个
.cpp代码:gist.github.com/alvations/99354d0fbe294c14cf7f,用来从这个软件moin.delph-in.net/ReppTop读取一系列.rpp文件:dropbox.com/s/ppgglwvwz16hlt1/erg.zip?dl=0。我们正在尝试移植此 python。 -
我的建议是不要尝试在 Python
re中进行这项工作。请改用regex包。在要点中,我看到输入被解释为正则表达式。用户输入的正则表达式可能会使用 Boost 中的高级功能,这在 Python re 中可能不可用。此外,如果您正在处理 Unicode,则应该使用 Python 3.3+。至于剩下的问题,太宽泛了。 -
regex_search的行为与regex_search应为regex_search的行为为re.search。我同意 nhahtdh 的观点,即这个问题很广泛,而且由于您没有共享所有的正则表达式,所以也不清楚(不知道它们有多复杂,以及它们是否可以按原样移植到 Python)。经验法则是不要盲目地在另一种语言中使用一种语言代码。 -
用于提升 Unicode (ICU) 正则表达式。使用 make_u32regex 的唯一原因是使用 ICU 属性库。在这种情况下,所有道路都通向 utf-32,无论输入正则表达式和主题编码是 utf8/16/32 是什么。对于宏语言,默认的 Unicode 内部编码通常是 utf-8,并且它们提供了自己的 Unicode 属性版本。所以,这些都不应该是一个问题。 可能的唯一问题是将任何特殊的 boost 构造转换为标准化形式。这可能意味着整理元素或其他元素。