Regex replace help

Richard Shaw hobbes1069 at gmail.com
Sun Dec 4 14:01:16 UTC 2011


2011/12/3 Miloslav Trmač <mitr at volny.cz>:
> On Sun, Dec 4, 2011 at 12:07 AM, Richard Shaw <hobbes1069 at gmail.com> wrote:
>> $ echo "const int TIXML_MAJOR_VERSION = 2;" | sed 's/const int
>> TIXML_MAJOR_VERSION = \([0-9]+\).*/\1/'
>> const int TIXML_MAJOR_VERSION = 2;
>> By replacing (sed 's/../../') with (sed -n 's/../../p') you can see
> that the regex doesn't match.  The cause is that sed recognizes an
> "\+" operator, not "+" operator.

Ahh, I didn't know that needed to be escaped, thanks!


On Sun, Dec 4, 2011 at 4:18 AM, Ville Skyttä <ville.skytta at iki.fi> wrote:
>> By replacing (sed 's/../../') with (sed -n 's/../../p') you can see
>> that the regex doesn't match.  The cause is that sed recognizes an
>> "\+" operator, not "+" operator.
>> ...and if you want portability, don't use + or \+ with sed at all to
> mean "one or more", use \{1,\} instead.

Not to worried about portability, it just needs to work in cmake.


On Sun, Dec 4, 2011 at 5:32 AM, Haïkel Guémar <karlthered at gmail.com> wrote:
> I guess you're using cmake string REGEX MATCH operation ? If that's the
> case, you can't.

No, just REGEX REPLACE. Here's a snippet of what I currently have in
my FindTinyXML module:

set(_tixml_header ${TINYXML_INCLUDE_DIR}/tinyxml.h)
file(READ ${_tixml_header} _contents)
if(_contents)
    string(REGEX REPLACE "^const int TIXML_MAJOR_VERSION[ \t]*=[
\t]*([0-9]+).*$" "\\1" _OUT_major "${_contents}")
    string(REGEX REPLACE "^const int TIXML_MINOR_VERSION[ \t]*=[
\t]+([0-9]+).*$" "\\1" _OUT_minor "${_contents}")
    string(REGEX REPLACE "^const int TIXML_PATCH_VERSION[ \t]*=[
\t]+([0-9]+).*$" "\\1" _OUT_patch "${_contents}")

The cmake documentation actually isn't very clear on what all it
supports other than the operators and anchors.
Can I use \s for whitespace?

I stole much of this from the FindGTK2.cmake module.

I assume the [ \t]* is supposed to match zero or more spaces or tabs,
but why do it that way unless \s isn't supported.

> 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}")

I think I see what you're doing here, but shouldn't a direct replace work?

Thanks,
Richard


More information about the devel mailing list