Help with regular expressions

Hiya

I would like some help with regular expressions to do the following:

I have lines of code which appear as following:

if ( variable1 == rhs.variable1 ) x = 2;

where "variable1" changes from line to line e.g. variable1 could be something like

if ( xyz== rhs.xyz) x = 2;

I want to replace all these lines with the following:

if ( allocate( variable1, rhs.variable1 ) ) x = 2;

where allocate is a function.

Can anybody tell me how to use regular expressions so I can do a replace on the source file so I can make the changes?

Thanks in advance

John
IssacJonesAsked:
Who is Participating?
 
käµfm³d 👽Commented:
OK. Change the syntax to:
Find:

    if:b*\({:i}:b*==:b*{:i\.\1}\):b*{:i:b*=:b*:a+:b*;}

Replace:

    if ( allocate( \1, \2 ) ) \3

-- if the above replace doesn't work, try

    if ( allocate( \1, \2 ) ) \3

Open in new window

0
 
käµfm³d 👽Commented:
Which IDE and version are you using?
0
 
käµfm³d 👽Commented:
I believe the following would help you, but it would be dependent on what you are using to do the find/replace (e.g. the IDE, external text editor, etc.). Some editors use their own special syntax for regexes. The pattern below is based on Perl regex patterns (which most other engines use).
Find:

    if\s*\(\s*(\w+)\s*==\s*(\w\.\1)\)\s*(\w+\s*=\s*\w+;)

Replace:

    if ( allocate( $1, $2 ) ) $3

-- if the above replace doesn't work, try

    if ( allocate( \1, \2 ) ) \3

Open in new window

0
 
IssacJonesAuthor Commented:
Hi kaufmed

Visual Studio 2008

John
0
 
käµfm³d 👽Commented:
Ignore the "-- if the above ..." part above....  I forgot to erase it  :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.