10SecTom
10SecTom

Reputation: 2664

Add certain amount of white space find replace in eclipse

I am playing around with c file editing, using eclipses find and replace. I started out trying to find all cases, where a pointer to an element in our array of Frames was being initialised (Frame = struct used in the code) . I then changed the expression to make this array 2 dimensional.

find = (.*Frame\*|.*pFrame)(.*Frame.*=.*)(&Frames)(\[.*\])(\w*)

replace = $1$2&Frames\[group\]$4

so statements like this

Frame* thisFrame = &Frames[frame]

and this

pFrame myFrame = &Frames[i]

become this

Frame* thisFrame = &Frames[group][frame]

and this

pFrame myFrame = &Frames[group][i]

I now want to add white space to a certain line length, then add a comment. For example something like "// indexing Frames".

pFrame myFrame = &Frames[group][i]                       // indexing Frames

Basically I want to clean up the (\w*) bit. Any help appreciated.

P.S. I am a regex noob, so any suggested edits for a cleaner way of doing the find are welcome

Edit: I would also like to edit the find to not include lines that start with //. I tried

([^//]+)(.*Frame\*|.*pFrame)(.*Frame.*=.*)(&Frames)(\[.*\])(\w*)

Upvotes: 1

Views: 52

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626845

I suggest

^((?:(?!//).)*(?:Frame\*|pFrame))(.*Frame.*=.*)(&Frames)(\[.*\])(\w*)

to replace with $1$2&Frames\[group\]$4 // indexing Frames.

Explanations:

  • ^ - start of the line
  • ((?:(?!//).)*(?:Frame\*|pFrame)) - Group 1 matching
    • (?:(?!//).)* - zero or more characters that are not a starting point for a // sequence
    • (?:Frame\*|pFrame) - Frame* or pFrame.
  • (.*Frame.*=.*) - 0+ any chars up to the last Frame, 0+ any chars, = and again 0+ any chars
  • (&Frames) - Literal &Frames
  • (\[.*\]) - Group 4 matching [, 0+ any chars and a ]
  • (\w*) - Group 5 matching 0+ word chars

Upvotes: 1

Related Questions