Maxter
Maxter

Reputation: 824

Visual studio Regex for batch replace

I am trying to find and replace some chars in Visual studio 2017. For exemple, I want to change this line:

rs.Fields("F1") = rs.Fields("F2"): 

Into this line:

rs.Fields["F1"].Value = rs.Fields["F2"].Value; 

So I need to replace according to this:

( --> [
) --> ].Value
: --> ;

First I select the line I want to change, then I find the chars I want to change with this regular expression:

([():])

This work fine. But I can't understand how to do the replace part properly.

Upvotes: 2

Views: 121

Answers (2)

Poul Bak
Poul Bak

Reputation: 10930

You can use the following regex:

(rs.Fields)\(([^)]*)\)(\s=\srs.Fields)\(([^)]*)\):

It starts by creating a Group, containing 're.Fields', followed by a left-parenthes, followed by any characters not being right-parenthes (in Group 1), followed by a right-parenthes. Then it creates a new group matching a White Space, followed by an equal sign and then the Whole thing repeated.

You then replace with the following:

$1[$2].Value$3[$4].Value

Upvotes: 1

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626927

You may use

Find: \b(rs\.Fields)\(([^()]*)\)(\s*=\s*)(rs\.Fields)\(([^()]*)\):
Replace: $1[$2].Value$3$4[$5].Value;

See the regex demo

Details

  • \b(rs\.Fields) - Group 1 (later referred with $1 from the replacement): rs.Fields substring (\b is a word boundary to match rs and not rs in, say, brs)
  • \( - a (
  • ([^()]*) - Group 2: any 0+ chars other than ( and )
  • \) - a )
  • (\s*=\s*) - Group 3: a = enclosed with optional whitespaces
  • (rs\.Fields) - Group 4: rs.Fields
  • \( - a (
  • ([^()]*) - Group 5: any 0+ chars other than ( and )
  • \) - a )
  • : - a colon.

Upvotes: 2

Related Questions