Using regular expressions in VisualStudio editor text substitution

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.