0

我在 boost 中使用 regex_search,如下所示:

std::string symbol = "abcd1234";

boost::regex regExpr("(\\d{4})", boost::regex::icase);

boost::smatch regMatch;

boost::regex_search(symbol, regMatch, regExpr);

我需要得到的是:“abcd”,即到第一个匹配的reg 表达式的原始字符串。这怎么可能?提前致谢...

4

1 回答 1

2

The following should work:

^(.*?)\\d{4}

Explanation:

^ - the start of the string
. - wild-card
.*? - zero or more (*) wild-cards (.), matched non-greedily (?), so you get the first match, not the last one

So you match everything from the start of the string to the digits.

Alternative using boost functionality:

regMatch.prefix() should return the required string.

于 2013-08-20T17:06:24.720 回答