[cmake-developers] regex replace bug?

David Cole david.cole at kitware.com
Tue Oct 30 18:12:54 EDT 2012

On Tue, Oct 30, 2012 at 5:45 PM, Matthew Woehlke
<matthew.woehlke at kitware.com> wrote:
> While writing a command to invoke asciidoc, I thought I would use this
> little regex replace to get the output file name:
>   string(REGEX REPLACE "([.][^.]+)?$" ".html" BAR "${FOO}")
> However, CMake is throwing this error:
>   string sub-command REGEX, mode REPLACE regex "([.][^.]+)?$" matched an
>   empty string.
> This doesn't seem right for two reasons:
> 1. The match (given an input 'foo.txt') should have been non-empty.
> 2. Even if the match is empty, the use of an anchor makes it definite and
> non-ambiguous, so replacement should succeed.
> For point 2, I get a similar error if the match is '^' or '$', both of which
> I would have expected to be valid. (Silly perhaps if/when the expression is
> known when writing the replace rule, as using a regex replace is overkill
> for such a case, but still valid.)
> For comparison, doing the replace with sed using the same expression works
> as expected (i.e. { 'foo', 'foo.txt' } -> 'foo.html'). In particular, while
> sed is understandably unhappy being asked to match and replace '' (empty
> pattern), '^' and '$' are both valid.
> Is this expected behavior, or should I file a bug?
> (And yes, I'm now using get_filename_component, but still...)
> --
> Matthew
> --
> Powered by www.kitware.com
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
> Please keep messages on-topic and check the CMake FAQ at:
> http://www.cmake.org/Wiki/CMake_FAQ
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/cgi-bin/mailman/listinfo/cmake-developers

You can file a bug for it..., and hopefully also propose a reasonable
patch to fix it.

The code in question (found in CMake/Source/cmStringCommand.cxx)
doesn't ever match an empty string. I thought that this was
documented, but I can't find the docs. It must be just something I
learned about CMake string command and matching stuff early on in my
CMake days, and have grown accustomed to.

So... it behaves as it has for quite some time now, although I can see
your point about wanting to match/replace on an anchored empty string.

David C.

More information about the cmake-developers mailing list