Bagelstein
Bagelstein

Reputation: 202

Find first match following another string, python regex multiline

I know many flavors of these questions have been asked, but I am having a bit of trouble getting a regex statement working to address my specific problem.

I have a large number of functions with different names but the exact same format and I need to find the first match following a specific function name.

Note that I am searching through a C file using python.

 writecwp_positionStatus(int      action,
        u_char   *var_val,
        u_char   var_val_type,
        size_t   var_val_len,
        u_char   *statP,
        oid      *name,
        size_t   name_len) {

static long     intval;
static long     old_intval;

switch ( action ) {
    case RESERVE1:
      if (var_val_type != ASN_INTEGER) {
          fprintf(stderr, "write to mib not ASN_INTEGER\n");
          return SNMP_ERR_WRONGTYPE;
      }
      if (var_val_len > sizeof(long)) {
          fprintf(stderr,"write to mib: bad length\n");
          return SNMP_ERR_WRONGLENGTH;
      }
    intval = *((long *) var_val);
      break;

    case RESERVE2:
      break;

    case FREE:
         /* Release any resources that have been allocated */
      break;

    case ACTION:
         /*
          * The variable has been stored in 'value' for you to use,
          * and you have just been asked to do something with it.
          * Note that anything done here must be reversable in the UNDO case
          */
        old_intval = starting_int;
        starting_int = intval;
      break;

    case UNDO:
         /* Back out any changes made in the ACTION case */
         starting_int = old_intval;
      break;

    case COMMIT:
         /*
          * Things are working well, so it's now safe to make the change
          * permanently.  Make sure that anything done here can't fail!
          */
      break;
} return SNMP_ERR_NOERROR;

}

In this example I want to find the first "old_intval = starting_int;" following the function name "writecwp_positionStatus". There will be many more functions with the same exact body but different names.

My thought was to set up a capture group to match:

(function name)(everything in between including newlines)(line to replace)

I tried a bunch of different options such as, but seem to be off by just a little each time:

(writecwp_positionStatus\(.*\s)((.*\s)*?)(\s*old_intval = starting_int;)

Upvotes: 1

Views: 82

Answers (1)

arif
arif

Reputation: 524

I would suggest this regex instead.

(writecwp_positionStatus[\s\S]*?)old_intval = starting_int;([\s\S]*)

Here, the approach is to capture everything from the function-name to the statement to be repalaced by capture group 01 and then match everything after the staement by capture group 02

\s -> whitespace character (a space, a tab, a line break, or a form feed).
\S -> non-white space character.
*? -> ? after quantifiers makes them lazy/non-greedy.

Now to replace the statement, we can use another regex:

\1 >>>I am the replacement<<< \2

Here,

\1 -> Everything before the statement.
\2 -> Everything after the statement.

To understand better, do experiments here. I hope this is what you wanted.

Upvotes: 2

Related Questions