escape sequence \\K for regular expression in boos

2019-08-01 13:30发布

问题:

I need to replace a look-behind expression with \K in boost (version 1.54) because of its limitation but it does not work. How can I do it or what is the problem? Is there any other way to convert this expression with lookahead?

"(?<=foo.*) bar" => "foo.*\K bar" ???

回答1:

Bit of a late answer here...

According to the Boost.Regex 1.54 Documentation, use of Perl's \K is possible, and I have just confirmed it via testing in Sublime Text 3, which uses Boost.Regex for its regex searching engine. Furthermore, I see no obvious syntactical error with either of the forms you posted. The only thing I can think of is that you're using the regex inside a string literal, and haven't escaped the \. If that's the case, the correct regex for your example would be:

foo.*\\K bar


If that's not the case, one workaround (that obviously has performance implications) is to reverse the string, and then use a variable-width look-ahead. The modified regex for your example would then be:

rab (?=.*oof)


回答2:

I believe the problem is that Boost lookbehind pattern must be of fixed length.

Your expression contains a repeat .* which makes it variable length.