2

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.

4
  • 6
    CMake has its own regex specification, and this specification lacks of character classes like \S and \w. Commented Sep 10, 2018 at 9:44
  • thank you. I have found a regex that works. Not sure holw good it is for all cases but from what I've tested seems to work Commented Sep 10, 2018 at 10:48
  • On Stack Overflow we tend to NOT answer on the question in the question post itself. Instead, you are allowed to answer on your question. Please, move your last Edit paragrapgh into the answer post. Commented Sep 10, 2018 at 11:51
  • done. sorry it took 2 weeks Commented Sep 24, 2018 at 8:23

2 Answers 2

4

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}\"")
Sign up to request clarification or add additional context in comments.

3 Comments

thanks but I was looking for smth smaller like a line of code no more :D
@Kennedy not stated in the question, but no worries. This is just 2 lines and uses no regex, so there might be some merit there; not sure what strings you're coming up against.
it worked for some examples i've tried in the interpreter. should be ok thanks :D
0

I have used this and it works(not sure how good it is for all cases, though):

.[(A-z)|(a-z)|(0-9)]*

2 Comments

You are almost certainly mixing two different regex features here. The stuff between [ and ] is simply an enumeration of characters, with no other special facilities than character ranges and negation. You are including ( and ) and | in the character class (multiple times), probably needlessly.
As tripleee says, you would use .[A-Za-z0-9]*

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.