Regular Expressions-what you must know

Source: Internet
Author: User

Many people do not want to modify the publicCodeBecause a change to the public code may affect other engineering codes, a large number of other engineering codes must be modified at the same time. For simple modifications, such as function name modification, you can use simple batch search and replacement. However, in more complex situations, many people are helpless and can only manually modify them one by one. EffectiveProgramThere is a story in CLERK:

"This occurs in a project where 1000 EJB (Enterprise Java Bean) is used ), at that time, an additional parameter was required to determine all non-EJB methods (that is, all methods except the callback method managed by EJB. It was estimated that it would take six days for a person to manually complete the process. However, a developer who is familiar with regular expressions can use his trusted Editor (Emacs) to complete all the replacements in just two hours. That is, at the beginning of that day, I decided to study regular expressions."

It can be imagined that it only takes two hours for a manual workload of six days. (Create the correct syntax in one hour and 58 minutes, and then execute it in less than two minutes .)

I. Learn Regular Expressions

Some people once summarized the Seven Weapons of programmers, including "Regular Expressions ". If you are still familiar with regular expressions, it takes two hours to carefully study and study the regular expression syntax rules. There are many resources to learn regular expressions. If you want to learn systematically, read 《Proficient in Regular ExpressionsAnd other books. If you want to get started quickly, I recommend a simple tutorial written by Jimmy Zhang, who is very popular recently in the park:

Learning Resources:

Http://www.cnblogs.com/jimmyzhang/archive/2007/10/24/936151.html (take 1-2 hours to learn carefully, and then practice repeatedly, can quickly get started)

Regular Expression tool:

I launched regexbuddy first. The following URL contains the full version of regexbuddy3.2.1 (if you carefully browse this website, you will find this person is a regular expression enthusiast ):

Http://iregex.org/blog/regexbuddy321.html

Ii. Instances

Next, let's go back to the problem of modifying the public database mentioned at the beginning. For example, we designed a game class and provided the start instance method as follows:

Public   Class Game
{
Public   Void Start ( Int Totaltime, String Gamename)
{
//  
}
}

 

This method is then used by a large number of other code. Until one day, we decided to change the order of the two parameters of the Start method, because gamename as the first parameter looks more pleasing to the eye... Then, we observe some code that uses the game class and find that this code is used in many places, but almost all the Code uses a method similar to the following to call the start method:

Game =   New Game ();
Game. Start ( 1 , " Nancytetris " );

Game mygame= NewGame ();
IntTotaltime= 10;
StringGamename= "Nancygline";
Mygame. Start (totaltime, gamename );

 

Of course, in actual situations, the code that calls this method is strange. In some complicated cases, it is really difficult to use a regular expression to achieve perfect batch replacement. Here, I will simplify the problem, just to provide you with an idea. I assume that all the code that calls this method uses the above method for calling. (To simplify Regular Expressions and make it easy to understand, I assume that the calling Code complies with the code specifications, so there will be no extra spaces or line breaks .)

So what kind of regular expression can replace the two parameters of the Start method of the game instance in the above Code?

Answer:

Search:Game (\ W +) = new game \ (\); ([\ W] *?) \ 1. Start \ ([\ W \ "] +), ([\ W \"] + )\);

Replace:Game $1 = new game (); $2 \ 1. Start ($4, $3 );

This example is common and useful. If you still don't know it, please take two hours to study and solve it!

In the actual batch search replacement process, you will certainly encounter a variety of troubles, because you will find that the Code calling style varies widely, such as the example above, someone may pass the game instance as a parameter to another function (or even this function is in another file), for example:

Public   Void Do ()
{
Game =   New game ();
Foo (game );
}< P>

// the foo method may be in another file
Public void Foo (game)
{< br> game. start ( 1 , " nancytetris " );
}

some people may even suffer from a headache if they do not comply with the code standards. You will find that a regular expression cannot do everything at once. At this time, you may need to write a script to perform more complex processing. If the regular expression can be used freely in your hands, do you feel that everything is so simple? If you have more experience in batch replacement of regular expressions, please share with us!

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.