【发布时间】:2011-12-21 20:03:38
【问题描述】:
这真的可行吗?我有一些很难理解的很长的正则表达式模式规则,因为它们不能立即融入屏幕。示例:
test = re.compile('(?P<full_path>.+):\d+:\s+warning:\s+Member\s+(?P<member_name>.+)\s+\((?P<member_type>%s)\) of (class|group|namespace)\s+(?P<class_name>.+)\s+is not documented' % (self.__MEMBER_TYPES), re.IGNORECASE)
反斜杠或三引号不起作用。
编辑。我结束了使用 VERBOSE 模式。下面是正则表达式模式现在的样子:
test = re.compile('''
(?P<full_path> # Capture a group called full_path
.+ # It consists of one more characters of any type
) # Group ends
: # A literal colon
\d+ # One or more numbers (line number)
: # A literal colon
\s+warning:\s+parameters\sof\smember\s+ # An almost static string
(?P<member_name> # Capture a group called member_name
[ #
^: # Match anything but a colon (so finding a colon ends group)
]+ # Match one or more characters
) # Group ends
( # Start an unnamed group
:: # Two literal colons
(?P<function_name> # Start another group called function_name
\w+ # It consists on one or more alphanumeric characters
) # End group
)* # This group is entirely optional and does not apply to C
\s+are\snot\s\(all\)\sdocumented''', # And line ends with an almost static string
re.IGNORECASE|re.VERBOSE) # Let's not worry about case, because it seems to differ between Doxygen versions
【问题讨论】:
-
re.VERBOSEexample -
@J.F. Sebastian:我不得不为 re.DEBUG 单独 +1,这将使我未来的生活变得更加轻松!
-
@JFSebastian:我在链接后面支持你的答案,因为最后我仍然使用它,即使它需要更多编辑(必须确保每个空格都被正确标记)。跨度>
-
cmets 的文字样式,例如
') # Group ends'不是很有用。我在我的示例中使用它只是为了回答相应的问题。在实际代码中,您应该假设读者已经知道()在正则表达式中的含义。逻辑与代码 cmets 相同。这是better example(注意:(?x)扮演re.VERBOSE的角色)。 -
顺便说一句,@N3dst4's answer 通过启用语法突出显示,为
(?x)提供了更好的替代方案。您也可以使用[ ]或\转义空格。