Use regular expressions in Visual Studio to match line breaks and replace them in batches

Source: Internet
Author: User

System Environment: Windows 8.1 Enterprise Update 2x64

Development Environment: Mircosoft Visual Studio Ultimate 2013 Update 2 RC

Q: How to use regular expressions in Visual Studio to match line breaks, blank spaces, Tab breaks, and other special symbols, and replace the code text you specified in the current document, the current project, or the entire solution in batches?

Example:

                                                                                        List<T> GetAllData<T>( tableName,  fieldShow,  fieldOrder,  )  T : ();

Answer:

Press Ctrl + Shift + H in Visual Studio IDE To open the [Replace in file] window,

In the {search content (N) :} text box, enter:

</Returns> [\ s] + [^ \ S \ r \ n] + List <T>
Or
</Returns> [\ r \ n] + [^ \ S \ r \ n] + List <T>

In the {Replace with (P) :} text box, enter:

</Returns> \ r \ n \ t [Obsolete ("this method is not recommended. Use the same signature method instead. ")] \ R \ n \ t \ tList <T>

In the {search range (L):} drop-down box, select the target range, because I want to replace all the matching items in the current project, so I chose "Current Project ".

Press {replace all (A)} to replace all the matching items with the content you want to replace.

This time I Replaced 67 files in batches, 67 rows.

Answer: This time I want to add a line between the end of the XML comment and the next word in the end.

[Obsolete()]

The end of XML is </returns>, and the START word of the next line at the end is List <T>, there are several invisible escape characters \ r \ n \ t between the two rows. However, if we write the regular expression as follows:

</Returns> [\ r \ n \ t] + List <T> or </returns> [\ r \ n] + [\ t] + List <T>

You will not be able to match any rows.

In Visual Studio, if you need to match line breaks and blank spaces, you must use [\ S \ s] and [^].

What does \ S \ s mean?

Uppercase \ S matches any non-blank characters.

Lowercase \ s matches any blank characters, including spaces, tabs, and break characters.

^ Is to pin the matching string to the first line or string.

+ Do not need to mention it? Is to match one or more matching items of the previous expression (and match as many characters as possible ).

Because the end of a line contains \ r \ n, you can use [\ r \ n] or [\ s] to determine the number of such special escape characters, use [\ r \ n] + or [\ s] +

The point is that to match the next line, that is, line feed, you also need to add a ^. When you use Visual Studio built-in formatting, although the first line is a \ t or above, you still need to use \ S instead. The combination is [\ S \ r \ n] +.

After splicing, we get </returns> [\ s] + [^ \ S \ r \ n] + List <T>.

In {Replace with (P):}, it is relatively simple and will not be detailed.

See Visual Studio official MSDN online help documentation: http://msdn.microsoft.com/en-us/library/2k3te2cs.aspx

 

Related Article

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.