Reputation: 317
I have a CMake string variable:
set(var "String0 String_1 String_2")
I need to select whatever is before the first whitespace from this variable ("String0"
) and make a new variable with this content.
I used CMake's REGEX MATCH
method to do this and used this regex: '\S\w*'
. I have tested that regex on an online regex interpreter and it worked.
I have written this code in CMake (after setting the variable of course):
string(REGEX MATCH "\S\w*" NEW_VAR "${VAR}")
When I do this the script complains about invalid skipping of characters (S
and w
). So, next, I tried escaping both the slashes:
string(REGEX MATCH "\\S\\w*" NEW_VAR "${VAR}")
Now NEW_VAR
equal 'S'
instead of "String0"
as I was expecting.
Please help me correct this, since I have very little experience with regular expressions and CMake.
Upvotes: 2
Views: 29329
Reputation: 317
I have used this and it works(not sure how good it is for all cases, though):
.[(A-z)|(a-z)|(0-9)]*
Upvotes: 0
Reputation: 2339
You could also search for the location of the first occurrence of a whitespace character and select the substring from the start of this string till that position:
set(var "String_0 String_1 String_2")
string(FIND ${var} " " wsloc)
message(STATUS "position of first whitespace character: ${wsloc}")
string(SUBSTRING ${var} 0 ${wsloc} newvar)
message(STATUS "selected substring: \"${newvar}\"")
Upvotes: 3