【发布时间】:2009-12-09 09:12:14
【问题描述】:
我正在尝试过滤数千个文件,寻找那些包含混合大小写的字符串常量的文件。此类字符串可以嵌入空格中,但本身可能不包含空格。所以以下(包含 UC 字符)是匹配项:
" AString " // leading and trailing spaces together allowed
"AString " // trailing spaces allowed
" AString" // leading spaces allowed
"newString03" // numeric chars allowed
"!stringBIG?" // non-alphanumeric chars allowed
"R" // Single UC is a match
但这些不是:
"A String" // not a match because it contains an embedded space
"Foo bar baz" // does not match due to multiple whitespace interruptions
"a_string" // not a match because there are no UC chars
我仍然想匹配包含 both 模式的行:
"ABigString", "a sentence fragment" // need to catch so I find the first case...
我想使用 Perl 正则表达式,最好由 ack 命令行工具驱动。显然,\w 和 \W 不会起作用。似乎 \S 应该匹配非空格字符。我似乎无法弄清楚如何嵌入“每个字符串至少一个大写字符”的要求......
ack --match '\"\s*\S+\s*\"'
是我得到的最接近的。我需要用 something 替换 \S+ 以捕获“至少一个大写(ascii)字符(在非空白字符串的任何位置)”要求.
这在 C/C++ 中编程很简单(是的,Perl,在程序上,无需借助正则表达式),我只是想弄清楚是否有一个正则表达式可以做同样的工作。
【问题讨论】:
-
“混合大小写”是否意味着它必须包含两个大写和小写字母?
-
其实没有。我添加了一个示例,“R”也应该匹配。谢谢。