0

我正在尝试解析可以包含文件路径的字符串。我正在使用带有正则表达式库的 C++。我对正则表达式不是很好,这里是 ECMAScript。

我不知道为什么字符串:

"C:\Windows\explorer.exe C:\titi\toto.exe"

不匹配模式(实际上它只找到第一个)

(?:[a-zA-Z]\:|\\)(?:\\[a-z_\-\s0-9]+)+

你有更好的主意来找到每场比赛吗?谢谢!

这是我的代码:

wsmatch matches;
regex_constants::match_flag_type fl = regex_constants::match_default ;  
regex_constants::syntax_option_type st = regex_constants::icase             //Case insensitive
                                        | regex_constants::ECMAScript
                                        | regex_constants::optimize;

wregex pattern(L"(?:[a-zA-Z]\\:|\\\\)(?:\\\\[a-z_\\-\\s0-9]+)+", st);

// Look if matches pattern
printf("--> %ws\n", path.c_str());
if (regex_search(path, matches, pattern, fl) 
&& matches.size() > 0)
{
    for (u_int i =  0 ; i < matches.size() ; i++)
    {
        wssub_match sub_match = matches[i];
        wstring sub_match_str = sub_match.str();

        printf("%ws\n", sub_match_str.c_str());
    }
}   
4

2 回答 2

0

You could use something like this:

.?:(\\[a-zA-Z 0-9]*)*.[a-zA-Z]*

I tested it with http://regexpal.com/ and it extracts all file paths.

于 2013-03-29T19:59:00.023 回答
0

虽然@mspoerr 提供的正则表达式满足了示例问题,但在更复杂的场景中它对我来说并不是很好,因此我曾经编写自己的。

正则表达式:

(\w:)?([\\\w\s0-9_]*)\.\w+

高级测试字符串:

C:\Wi ndows\explorer.exe asdasds
 : ad  C:\titi\toto.Heexe
HELLOO : qwefqwfqwf c:\aa.

(它只匹配两个有效的文件路径)

于 2014-05-13T14:26:33.147 回答