[cmake-developers] regex bug?

Matthew Woehlke matthew.woehlke at kitware.com
Thu Dec 13 13:55:50 EST 2012


On 2012-12-13 13:44, David Cole wrote:
> On Thu, Dec 13, 2012 at 1:38 PM, Matthew Woehlke wrote:
>
>> Consider this line of CMake code:
>>
>> string(REGEX REPLACE "^([^.]*)(\..*)?$" "\1" BAR "${FOO}")
>>
>> It looks reasonable to me, and works fine on !Windows. However, on
>> Windows, I get these errors:
>>
>> Syntax error in cmake code at <elided> when parsing string
>> ^([^.]*)(\..*)?$
>> Invalid escape sequence \.
>>
>> Syntax error in cmake code at <elided> when parsing string
>> \1
>> Invalid escape sequence \1
>
> For the replacement string, you need to use "\\1" -- the backslash in that
> context needs to be escaped to get through the CMake language, so that the
> regex engine sees "\1" internally...

I tried that, but then, as per...

>> Also, I can use "^([^.]*)([.].*)?$" instead to get around the first error,
>> but then I get an error that the capture is empty. (It shouldn't be;
>> "${FOO}" is non-empty and the pattern is guaranteed to have a non-ambiguous
>> match for non-empty input.)

...I get this error. (More specifically, 'string sub-command REGEX, mode 
REPLACE regex "^([^.]*)(\..*)?$" matched an empty string.'.) Which isn't 
possible if the input is non-empty; as it is anchored on both ends, it 
must match the entire input, or not match at all.

-- 
Matthew




More information about the cmake-developers mailing list