[CMake] Problems with REGEX REPLACE

Pau Garcia i Quiles pgquiles at elpauer.org
Mon Mar 16 06:14:19 EDT 2009


Hi Wim,

Try this:

FOREACH(file a.js b.js)
  FILE(READ ${file} f0 )
  SET( f1 "char* myFile = \r\n${f0}" )
  STRING( REGEX REPLACE "\"" "\\\\\"" f2 "${f1}" )
  STRING( REGEX REPLACE "\r?\n" "\"\r\n\"" f3 "${f2}" )
  STRING( REGEX REPLACE "\r?\n.$" ";" f4 "${f3}" )
  STRING( REGEX REPLACE " = ." " = " f5 "${f4}" )
FILE(WRITE ${file}.cc "${f3}")
ENDFOREACH(file)

Regular expressions with CMake are tricky :-)

The problem with \" is you needed to use \\\\\" instead

The problem with ^ and $ not matching beginning-of-line and
end-of-line for multiline cases is bug 5380 ( see
http://public.kitware.com/Bug/view.php?id=5380 ) and the work-around
is replacing CRLF (see this thread:
http://www.mail-archive.com/cmake@cmake.org/msg07252.html )

On Sat, Mar 14, 2009 at 4:31 PM, Wim Dumon <wim at emweb.be> wrote:
> Hi,
>
> I have some files which contain lines like this:
> int a() {
>  print "foo";
>  do "bar";
> }
> Which I want to convert into a C-style string, so I want to start with
> some transformations that would turn this file into something like:
> char *myFile =
> "int a() {"
> "  print \"foo\";"
> "  do \"bar\";"
> "}";
>
> Currently we're using a small C++ program that does this, but for
> cross-compilation simplicity I was considering to replace this with a
> CMake script. I managed to write this transformation in sed, but not
> in cmake, and after a short look at the cmake code
> (cmStringCommand.cxx), I gave up. Is what I want possible with cmake?
>
> The errors produced are similar to:
>   string sub-command REGEX, mode REPLACE: Unknown escape "\""in
>   replace-expression.
>
>   string sub-command REGEX, mode REPLACE regex "^" matched an empty string.
>
> Best regards,
> Wim.
>
> cmake_minimum_required(VERSION 2.6)
>
> # The equivalent of: sed s/\"/\\\\\"/g | sed s/^/\"/ | sed s/$/\"/
> FOREACH(file a.js b.js)
>  FILE(READ ${file} f0)
>  # Does not work: \" is not valid in replacement string
>  STRING( REGEX REPLACE "\"" "\\\"" f1 "${f0}")
>  # Does not work: ^ matches an empty string and is for obscure reasons invalid
>  # Additionally, \" is not valid in replacement string
>  STRING( REGEX REPLACE "^" "\"" f2 "${f1}")
>  # Does not work: $ matches an empty string and is for obscure reasons invalid
>  # Additionally, \" is not valid in replacement string
>  STRING( REGEX REPLACE "$" "\"" f3 "${f2}")
>  # Alternative: does not work: \" is not a valid escape in replacement string
>  #STRING( REGEX REPLACE "\n" "\"\n\"" f2 "${f1}")
>  FILE(WRITE ${file}.cc "${f3}")
> ENDFOREACH(file)
> _______________________________________________
> 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://www.cmake.org/mailman/listinfo/cmake
>



-- 
Pau Garcia i Quiles
http://www.elpauer.org
(Due to my workload, I may need 10 days to answer)


More information about the CMake mailing list