The advantage of using regular expressions when replacing is that they can be captured by regular grouping and used in replacement strings.
In versions prior to VS2012, the capture content was included with {}, referenced using the \1 form;
In VS2012 and later versions, the capture content conforms to the regular expression, with () included, using the form of the reference.
The following is an example of the new version format:
Suppose your code contains multiple automatic attributes, such as
1 Public BOOL Get Set ; } 2 Public Double Get Private Set ; } 3 Private int Get set; }
Assume that the type implements the INotifyPropertyChanged interface, and that there are methods
1 void Raisepropertychangedevent (string PropertyName) {2 propertychanged?. Invoke (Thisnew PropertyChangedEventArgs (PropertyName)); 3 }
Now to add the appropriate private fields for all public properties so that they can invoke the function trigger property notification event when the assignment changes, you can call out the replacement window in Ctrl + H and enable the regular expression.
In the Source text box, enter:
Public (\s*) (\s*) {get; (\s*\s?) Set }
In the Replace target text box, enter:
Public $ {get {return _ $;} $ Set {_ $ = value; Raisepropertychangedevent ("$"); }} $;
In the matching text, each grouping is enclosed in parentheses () and is captured starting at 1. The attribute type is marked red, the property name is marked as green , and the set accessor modifier is marked purple .
In the target text, the captured text is represented by the dollar sign $ and 1 base index values, and the replacement code adds a private field for each public property, which is named preceded by an underscore _ in front of the property name.
1 Public BOOLIsChecked {Get{return_ischecked; }Set{_ischecked = value; Raisepropertychangedevent ("IsChecked"); } }BOOL_ischecked;2 Public DoubleWidth {Get{return_width; }Private Set{_width = value; Raisepropertychangedevent ("Width"); } }Double_width;3 Private intCount {Get;Set; }
Using regular expressions in VisualStudio editor text substitution