Regex replace help

Haïkel Guémar karlthered at gmail.com
Sun Dec 4 11:32:27 UTC 2011


Le 04/12/2011 00:07, Richard Shaw a écrit :
> I'm writing my own cmake module for finding TinyXML and I'm trying to
> extract the version from the header.
>
> For some reason it's matching the whole file no matter what I do. I've
> looked through several of the cmake modules in
> /usr/share/cmake/Modules and don't see what I'm doing that's so
> different.
>
> The relevant lines in /usr/include/tinyxml.h are:
>
> const int TIXML_MAJOR_VERSION = 2;
> const int TIXML_MINOR_VERSION = 6;
> const int TIXML_PATCH_VERSION = 1;
>
> To speed up the testing I'm trying to come up with a sed equivalent to
> make sure it's returning the version numbers properly.
>
> Here's what I have:
> $ echo "const int TIXML_MAJOR_VERSION = 2;" | sed 's/const int
> TIXML_MAJOR_VERSION = \([0-9]+\).*/\1/'
> const int TIXML_MAJOR_VERSION = 2;
>
> The "echo" is one of the lines I'm trying to match while only
> returning the version number. For some reason I'm getting the whole
> line back...
>
> Can anyone give me a hint before I pull all my hair out?
>
> Thanks,
> Richard

I guess you're using cmake string REGEX MATCH operation ? If that's the
case, you can't.
Here's a snippet that might help you:

cmake_minimum_required(VERSION 2.8)
project(test NONE)

set(INPUT "const int TIXML_MAJOR_VERSION = 89")
string(REGEX MATCH "const int TIXML_MAJOR_VERSION = [0-9]+" TMP ${INPUT})
# CMake regex doesn't have non-greedy quantifiers
# alternative  expression: "[^0-9]*([0-9]+).*"
string(REGEX REPLACE ".* ([0-9]+).*" "\\1" TIXML_MAJOR_VERSION "${TMP}")
message(WARNING "TMP: ${TMP} -- MAJOR_VERSION: ${TIXML_MAJOR_VERSION}")

best regards,
H.


More information about the devel mailing list