Trimming newlines from a string

Brad King brad.king at kitware.com
Tue Mar 21 22:56:03 CET 2006


Pino Toscano wrote:
> Hello,
> 
> finally, I'm subscribed too! :)
> 
> First mail, first question: imagine I have a string made by a newline 
> character only, ie the line
> message(STATUS "myvar: \"${myvar}\"")
> would output
> -- Found ...
> -- myvar: "
> "
> -- Found ...
> Doing a
> STRING(REGEX REPLACE "\\n?\\r?" "" myvar "${myvar}")
> have the result to make cmake output that the regexp matches an empty string.
> 
> Do you have any precious suggestion about sometimg that I'm missing?

Using the question-mark notation in regular expressions says that the 
character is optional.  Since you have it on both characters then the 
empty string is matched by taking the "no" option for each one.

To remove all trailing newlines try this:

STRING(REGEX REPLACE "(\r?\n)+$" "" myvar "${myvar}")

-Brad


More information about the Kde-buildsystem mailing list