Fantastic Mr Fox
Fantastic Mr Fox

Reputation: 33904

Why does a regex match the end of a group to the second match?

I am confused about why the following regex:

CHANGES:(.|\n)*(\*\/)

matches to the second comment closing (*/)instead of the first in the following block:

 /* ysqwwqdeqwd 
 Some general start comments and code description

 DESCRIPTION:

    Interface for c

 CHANGES:

    $Log: blala.h,v $
    Revision 1.7  2008/09/08 18:34:43  p
    Updated copyright year.
 */

#define startofcode yeah

/* General include files for Object Oriented C code.
 */
#include "oo.h"
#include "const.h"
#include "libmath.h"

here we would get this:

CHANGES:
    ...

 */

#define startofcode yeah

/* General include files for Object Oriented C code.
 */

instead of just:

CHANGES:
        ...

     */

here is a live example. The background here is that i am trying to remove a bunch of old CVS style svn commit logs from the top of a bunch of .h files that are no longer needed.

Upvotes: 1

Views: 30

Answers (1)

vks
vks

Reputation: 67978

CHANGES:(.|\n)*?(\*\/)

              ^^

You need non greedy regex.See demo.When you use greedy regex it will stop at the last instance of */.When you use non greedy it will stop at the first instance of */.* is greedy and will consume as much as it can.

https://regex101.com/r/vH0sZ0/3

Upvotes: 2

Related Questions