[CMake] REGEX ^ and $ do not match on multi-line <input>

Mathieu Malaterre mathieu.malaterre at gmail.com
Sun Jul 22 03:04:15 EDT 2007


Hi Brandon,

  I think you can use this trick to work around your issue:

http://www.cmake.org/pipermail/cmake/2007-May/014317.html

  STRING(REGEX REPLACE "\r?\n" ";" ENT "${input}")
  FOREACH(line ${ENT})
  # do the match on each line

HTH
-Mathieu

On 7/21/07, Brandon Van Every <bvanevery at gmail.com> wrote:
> I filed this as bug #5380.  Repeating it here in case anyone looks
> through the archives for a solution to this problem.
> http://cmake.org/Bug/bug.php?op=show&bugid=5380&pos=17
>
> # ^ and $ appear to be non-functional in practice. This makes it
> # impossible to code a non-trivial regex in CMake script.  This in turn
> # forces the user to find or install other tools that can do so, rather
> # than keeping their scripting logic self-contained in CMake.
> #
> # ^ and $ work work with respect to an entire <input> to STRING().
> # That is to say, an <input> is treated as one line.  The <input>
> # does not preserve newlines, even if it is read from a multi-line file.
> # The following code snippet demonstrates that ^ and $ will only
> # match at the beginning and end of a file, when the file is read in as
> # an <input> string.
>
> FILE(WRITE in.txt
> "line0
> line1
> line2")
>
> FILE(READ in.txt stream)
>
> STRING(REGEX MATCH "^line0" line0_start "${stream}")
> STRING(REGEX MATCH "line0$" line0_end "${stream}")
> STRING(REGEX MATCH "^line1" line1_start "${stream}")
> STRING(REGEX MATCH "line1$" line1_end "${stream}")
> STRING(REGEX MATCH "^line2" line2_start "${stream}")
> STRING(REGEX MATCH "line2$" line2_end "${stream}")
>
> MESSAGE("${line0_start}")
> MESSAGE("${line0_end}")
> MESSAGE("${line1_start}")
> MESSAGE("${line1_end}")
> MESSAGE("${line2_start}")
> MESSAGE("${line2_end}")
>
> #Output: only line0_start and line2_end match.
> #
> #C:\in\cbugs>cmake -P regex.cmake
> #line0
> #
> #
> #
> #
> #line2
> #
> #C:\in\cbugs>
> _______________________________________________
> CMake mailing list
> CMake at cmake.org
> http://www.cmake.org/mailman/listinfo/cmake
>


-- 
Mathieu


More information about the CMake mailing list