22

I need to write regex in cmake lists to replace all ends of lines to spaces. I tried this, but it is incorrect

STRING(REGEX REPLACE "/\s+/g" " " output ${input})
2
  • In what way is it "incorrect"? And what do you mean by "ends of lines"? It looks like that regex replaces groups of whitespace characters with a single space each. Commented Mar 25, 2014 at 15:15
  • string(REGEX REPLACE "[\r\n]*" " " output ${input}) Commented Mar 16, 2018 at 21:33

2 Answers 2

41

The command expects a regular expression, but you're passing a sed argument in.

If you really want to replace all line-end characters with spaces, there's even no need for a regex at all. Just do this:

string(REPLACE "\n" " " output ${input})
Sign up to request clarification or add additional context in comments.

2 Comments

What about \r ? or \r\n ?
@shuva Depends on how you set the contents of input, of course. But CMake file I/O is text-based by default, so if input was read by CMake from a file native to the system on which you're running, you shouldn't encounter \r at all.
3

It is possible to do that by

string(REGEX REPLACE "[\r\n]*" " " output ${input})

Interestingly a relevant problem was to convert it into a list like the following,

string(STRIP ${input} stripppedinput)
string(REGEX REPLACE "[\r\n]*" ";" output ${strippedinput})

1 Comment

It is better to put "[\r\n]+" to avoid error message that an empty string is matched.

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.