[CMake] Capturing matches in regex groups

Michael Hertling mhertling at online.de
Mon Nov 28 21:08:03 EST 2011


On 11/28/2011 11:35 PM, Robert Dailey wrote:
> I haven't really seen a way to get a list of group matches in a regex. For
> example, string( REGEX MATCH ) only returns the whole string matched, not
> just what was in the capture groups. If I do this:
> 
> (\\w+)\\,(\\w+)\\,(\\w+)
> 
> and I match that regex against this string:
> 
> hello,world,today
> 
> I should get a list with:
> 
> hello;world;today
> 
> How can I do this?

CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)
PROJECT(REGEXREPLACE NONE)
STRING(REGEX REPLACE
    "([A-Za-z0-9_]+),([A-Za-z0-9_]+),([A-Za-z0-9_]+)"
    "\\1;\\2;\\3" RESULT "hello,world,today")
LIST(LENGTH RESULT n)
MESSAGE("n=${n}")
FOREACH(i IN LISTS RESULT)
    MESSAGE("${i}")
ENDFOREACH()

Regards,

Michael


More information about the CMake mailing list