MantisBT - CMake
View Issue Details
0013310CMakeCMakepublic2012-06-17 15:202016-06-10 14:31
don la dieu 
Kitware Robot 
lowminoralways
closedmoved 
CMake 2.8.8 
 
0013310: Regular expression documentation doesn't match command effects.
The documentation for string(REGEX ...) says:

   () Saves a matched subexpression, which can be referenced
             in the REGEX REPLACE operation. Additionally it is saved
             by all regular expression-related commands, including
             e.g. if( MATCHES ), in the variables CMAKE_MATCH_(0..9).

This doesn't match the behavior of file(STRINGS ... REGEX ...)!

$ echo foo > /tmp/foo
$ cat CMakeLists.txt
file(STRINGS "/tmp/foo" tmpvar REGEX "^(foo)")
message(STATUS "*** ${CMAKE_MATCH_1} = foo")
$ cmake .

results in:
-- *** = foo

not
-- *** foo = foo
as expected.
No tags attached.
diff cmake_source_cmFileCommand.cxx.diff (1,090) 2012-06-18 16:54
https://public.kitware.com/Bug/file/4363/cmake_source_cmFileCommand.cxx.diff
Issue History
2012-06-17 15:20don la dieuNew Issue
2012-06-17 16:06Eric NOULARDNote Added: 0029711
2012-06-18 14:07don la dieuNote Added: 0029723
2012-06-18 14:52Eric NOULARDNote Added: 0029724
2012-06-18 14:53Eric NOULARDNote Edited: 0029724bug_revision_view_page.php?bugnote_id=29724#r684
2012-06-18 14:53Eric NOULARDNote Edited: 0029724bug_revision_view_page.php?bugnote_id=29724#r685
2012-06-18 14:55Eric NOULARDNote Added: 0029725
2012-06-18 16:54don la dieuNote Added: 0029734
2012-06-18 16:54don la dieuFile Added: cmake_source_cmFileCommand.cxx.diff
2012-06-18 17:17Eric NOULARDNote Added: 0029735
2012-08-11 21:42David ColeStatusnew => backlog
2012-08-11 21:42David ColeNote Added: 0030445
2016-06-10 14:28Kitware RobotNote Added: 0042068
2016-06-10 14:28Kitware RobotStatusbacklog => resolved
2016-06-10 14:28Kitware RobotResolutionopen => moved
2016-06-10 14:28Kitware RobotAssigned To => Kitware Robot
2016-06-10 14:31Kitware RobotStatusresolved => closed

Notes
(0029711)
Eric NOULARD   
2012-06-17 16:06   
You are expecting that the documentation from string(REGEX ...)
match the one from file(STRINGS ...).

This not the case.
file(STRINGS ...) documentation says:

       REGEX specifies a regular expression that a string must match to be
       returned. Typical usage

         file(STRINGS myfile.txt myfile)

       stores a list in the variable "myfile" in which each item is a line
       from the input file.

where did you see in that that you should get something in CMAKE_MATCH_x ?

The doc says you get the matched **lines** in the var "myfile".

So
string(REGEX ...) does not do the same thing as file(STRINGS ... REGEX ...).
I don't think it's a bug but a feature,
how would you get something in CMAKE_MATCH_x when multiple lines of the file
may match with different part?
(0029723)
don la dieu   
2012-06-18 14:07   
> You are expecting that the documentation from string(REGEX ...)
> match the one from file(STRINGS ...).

You bet. To quote the documentation from string(REGEX ...), again,

  () Saves a matched subexpression, which can be referenced
             in the REGEX REPLACE operation. Additionally it is saved
             by ***all regular expression-related commands***, including
             e.g. if( MATCHES ), in the variables CMAKE_MATCH_(0..9).

(emphasis added). So, it's not unreasonable to expect that a "regular expression-related command" like file(STRINGS ...) is included in "all".

> how would you get something in CMAKE_MATCH_x when multiple lines of the file
> may match with different part?

That has nothing to with the bug report, but it can be handled easily. You handle that like any other programming language with capturing parentheses handles that. For example in perl, file(STRINGS filename variable REGEX regex) is functionally equivalent to:

my $variable = "";
open(my $fh, "<", filename);
while(<$fh>) {
  if (/regex/)
    $variable .= $_
}

Assuming that "regex" has capturing parentheses, the last capture is stored in the capture variables. I am more than happy to provide more examples in other languages if you require further edification. But, the easiest and most direct way to address the bug report would be to update the documentation so that it matched command effects.
(0029724)
Eric NOULARD   
2012-06-18 14:52   
(edited on: 2012-06-18 14:53)
My bad for missing the "all regular expression-related commands".

Concerning the "CMAKE_MATCH_x when multiple lines of the file may match"
I may be again missing something but:

file(STRINGS toto.txt L REGEX "^(foo)")
message("L = ${L}, CMAKE_MATCH_1=${CMAKE_MATCH_1}")

is supposed to read "toto.txt" line by line and for each line which
match 'foo' store a new item in L.
so that for, toto.txt whose content is:
foo
goo
foo_1
goo_2
 fooooo

you get:
L = foo;foo_1, CMAKE_MATCH_1=

in this case what do you expect to found in CMAKE_MATCH_1
foo
foo_1 ?

in your perl example you have access to the loop over the lines of the file
which is NOT the case for file(STRINGS toto.txt L REGEX "^(foo)")
May be it's doable in this case (because the match is literal 'foo')
but what if we tried to match "^(foo.?.?)" ?

If you want to do similar things with CMake that you did with perl,
you can do:

file(STRINGS toto.txt L)
foreach(LINE IN LISTS L)
  if ("${LINE}" MATCHES "^(foo.?.?)")
    message("CMAKE_MATCHE_1=${CMAKE_MATCH_1}")
  endif()
endforeach()

in this case the definition of the content of "CMAKE_MATCH_1" is feasible.

May be I still miss something and would be pleased if you give me an example
of something I didn't catch.

(0029725)
Eric NOULARD   
2012-06-18 14:55   
Just a last remark.
In the case I'm right your are right too may be the documentation
for file(STRINGS ... REGEX) should be updated.
(0029734)
don la dieu   
2012-06-18 16:54   
Given your file of:
foo
goo
foo_1
goo_2
 fooooo

and the CMake lines:
file(STRINGS toto.txt L REGEX "^(foo)")
message("L = ${L}, CMAKE_MATCH_1=${CMAKE_MATCH_1}")

I'd expect the output to be:
L = foo;foo_1, CMAKE_MATCH_1=foo

because the parens captured 'foo'. Changing the file() command to:
file(STRINGS toto.txt L REGEX "^(foo.?.?)")

I'd expect the output to be:
L = foo;foo_1, CMAKE_MATCH_1=foo_1

I have a minor patch that implements the behavior I was expecting. Testing was extremely minimal though.
(0029735)
Eric NOULARD   
2012-06-18 17:17   
OK,
thank you for the clarification and patch proposal.

I suggest you raise the issue on the CMake developer mailing list
in order to see if others do have objection concerning your proposal.
(0030445)
David Cole   
2012-08-11 21:42   
Sending old, never assigned issues to the backlog.

(The age of the bug, plus the fact that it's never been assigned to anyone means that nobody is actively working on it...)

If an issue you care about is sent to the backlog when you feel it should have been addressed in a different manner, please bring it up on the CMake mailing list for discussion. Sign up for the mailing list here, if you're not already on it: http://www.cmake.org/mailman/listinfo/cmake [^]

It's easy to re-activate a bug here if you can find a CMake developer who has the bandwidth to take it on, and ferry a fix through to our 'next' branch for dashboard testing.
(0042068)
Kitware Robot   
2016-06-10 14:28   
Resolving issue as `moved`.

This issue tracker is no longer used. Further discussion of this issue may take place in the current CMake Issues page linked in the banner at the top of this page.