[CMake] Regex help: multi-line matching and matching backslashes

David Cole david.cole at kitware.com
Tue Jan 11 07:33:34 EST 2011


Thanks, Fraser. Of course...

If you need to escape a backslash such that the regular expression itself
contains a backslash at a certain point, then you need to double it up. For
example to match the backslash character itself, you need to use "[\\]", but
to encode other escape chars directly that CMake knows about, you only need
one. (Just as you only need one to put the real "\n" character into the
string in the first place.

This code shows that "Matches2" contains your expected results:


cmake_minimum_required(VERSION 2.8)

set(contents "Hello\nWorld!")
message("contents='${contents}'")

string(REGEX MATCHALL "Hello[\\r\\n\\t ]*World!" matches ${contents})
message("Matches1:")
foreach(match ${matches})
  message("match='${match}'")
endforeach()

string(REGEX MATCHALL "Hello[\r\n\t ]*World!" matches ${contents})
message("Matches2:")
foreach(match ${matches})
  message("match='${match}'")
endforeach()



On Mon, Jan 10, 2011 at 5:38 PM, Fraser Hutchison <
fraser.hutchison at googlemail.com> wrote:

>  I think if you remove the double "\\" it should work, i.e. use:
>
> string (REGEX MATCHALL "TEST_?F?\\([A-Za-z_0-9]+,[\r\n\t\\\\]*[A-Za-z_0-9
> ]+\\)" found_tests ${contents})
>
> Cheers,
>
> Fraser.
>
>
>
>
>
> On 1/10/2011 5:04 PM, Ben Medina wrote:
>
> It doesn't seem to work:
>
> cmake_minimum_required (VERSION 2.8)
>
> set (contents "Hello\nWorld!")
> message ("${contents}")
> string (REGEX MATCHALL "Hello[\\r\\n\\t ]*World!" matches ${contents})
>
> message ("Matches:")
> foreach (match ${matches})
>     message ("${match}")
> endforeach ()
>
> This produces no matches. If you replace "\n" with " " in the contents
> string, then you get a match.
>
> Thanks,
> Ben
>
> On Fri, Jan 7, 2011 at 12:18 PM, David Cole <david.cole at kitware.com> <david.cole at kitware.com> wrote:
>
>  It should work.
>
> But I'm pretty sure we don't recognize "\s" for white space. Try "[
> \\t\\n\\r]" instead of \\s.
>
> But..... watch out for white space after "(" and before ")" too. You might
> miss some lines if they have spaces there.
>
>
> On Fri, Jan 7, 2011 at 2:55 PM, Ben Medina <ben.medina at gmail.com> <ben.medina at gmail.com> wrote:
>
>  I need to parse a C++ file for Google Test macros. (I'm aware the
> GTEST_ADD_TESTS provided by FindGtest.cmake, but I need the test list
> for my own purposes). I had been using a regex similar to the one in
> GTEST_ADD_TESTS to match tests:
>
> string (REGEX MATCHALL "TEST_?F?\\([A-Za-z_0-9 ,]+)\\)" found_tests
> ${contents})
>
> But this doesn't work when the test is split onto two lines, like this:
>
> TEST(SampleTest,\
>     MultilineTest)
>
> So, I've been trying to build a regex that will match tests split onto
> multiple lines. This should work:
>
> string (REGEX MATCHALL "TEST_?F?\\([A-Za-z_0-9
> ]+,[\\s\\\\]*[A-Za-z_0-9 ]+\\)" found_tests ${contents})
>
> After the comma, the regex should greedily match all whitespace
> (including newlines) via the "\\s" and backslashes via the "\\\\"
> (Note that all special characters are escaped). This works in other
> regex engines, but fails in CMake.
>
> Is this possible in CMake, or do I need to use another tool?
>
> Here is my test file (named tests.cpp):
>
> TEST(SampleTest, SingleLineTest)
> TEST(SampleTest,\
>     MultilineTest)
> TEST_F(SampleTest, SingleLineFixtureTest)
> TEST_F(SampleTest,\
>     MultilineFixtureTest)
>
> And my CMakeLists.txt:
>
> cmake_minimum_required (VERSION 2.8)
>
> file (READ "tests.cpp" contents)
> string (REGEX MATCHALL "TEST_?F?\\([A-Za-z_0-9
> ]+,[\\s\\\\]*[A-Za-z_0-9 ]+\\)" found_tests ${contents})
> message ("Found tests:")
> foreach (test ${found_tests})
>    message ("${test}")
> endforeach ()
>
> Thanks,
> Ben
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects athttp://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
>
>   _______________________________________________
> 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
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cmake.org/pipermail/cmake/attachments/20110111/3d47e5ab/attachment-0001.htm>


More information about the CMake mailing list