Use regular expressions to find and replace

Source: Internet
Author: User
Tags posix
Use regular expressions to find and replace

First of all, I need to declare that the application of the regular is not particularly skilled, but in the work of "be forced helpless" only one step to the regular have some understanding. As we learn more about regular expressions, it is becoming more and more important to find that regular expression is really a powerful tool that uses regular, often with less effort.

There are a lot of general-purpose regular on the internet, such as finding the regular of phone numbers, finding the regular of email. I believe there are many friends like me, learning is from these popular regular start, when the experience of the strong, and these popular is no longer suitable, there is the motivation to learn the regular. In fact, the basic rules of the regular is very simple, easy to get started, but how to use it after the introduction is the same. PHP currently supports a total of two types: POSIX extended Regular Expressions and Perl-compatible regular expressions. Many PHP textbooks use POSIX extended regular expressions, but I prefer Perl-compatible regular expressions, one because there is better compatibility, and the other is that I think Perl-compatible regular expressions look clearer.

First, assume a working environment: there is a file containing user information, a total of 10,000 lines, each line records a user's information, the format is as follows: username,010-12345678,firstname.lastname,05/21/2007

In the following article, I will describe the lookup and substitution of Perl-compatible regular expressions in PHP in this hypothetical working environment.

Find

The most common lookup is preg_match (), the function is described as follows: Int Preg_match_all (string pattern, string subject, array matches [, int flags] syntax for regular expressions not much to say , I assume that the friends who read this article have a certain regular basis. In fact, the use of regular expression lookup is not very large, not too complex to find through the STRSTR () function to be realistic, and more efficient. Using regular lookups is often a relatively complex lookup that cannot be implemented with STRSTR (). For example, I'm looking for a phone number with an area code of 010 and LastName is a one-line record for Bill, so write Preg_match ('/^[^,]*,010[^\. *\.bill.*$/i ', $line); Where $line means a row of data for a file. If the area code in $line is 010,lastname and it is bill, the above statement will return a nonzero positive integer, usually we do not care about the value of the number, but only care if there is a match. And if we want to find out that LastName is the record of Bill's users in 2007, you can use the following statement Preg_match ('/^[^,]*,[^,]*,[^\. *\.bill,[^\/]*\/[^\/]*\/2007/i ', $line); The replacement of the regular expression is usually in the need to match two or more than two keywords, and these two keywords are not adjacent to each other. It is not possible to use normal lookup functions at this time.ReplaceIn contrast to lookups, I think substitution is the most powerful and useful place for regular expressions. Suppose we now need to change the date format of the 10,000 records in the file to Yyyy/mm/dd, what would you do? You will find that the normal search and replace is not up to this goal. You might say that you can decompose a row of records, analyze them, and reorganize them. I admit that this is really a solution, but this is not the best solution, let us look at how the regular is to achieve our requirements: $line = Preg_replace ('/([^,]*,[^,]*,[^,]*,) ([0-9]*)/([0-9]*) \/([0-9]*)/I ', "\ $usup \${4}\${2}\${3}", $line); Here we use the "sub-pattern" in the regular expression, careful observation can be found that the first parameter of the Preg_replace function in a total of four pairs ' () ', each pair of ' () ' content is a "sub-mode", in the second argument can be through \ $usup, \${2} Such a format to casually combine these sub-patterns. If I want to delete the phone number in the data, it can be written as follows: $line = Preg_replace ('/([^,]*,) ([^,]*,) [^,]*,) ([0-9]*]/([0-9]*]/([0-9]*)///, "\ $usup \${4}\ ${2}\${3} ", $line); Here, in order to introduce the sub-schema and use a more complex notation, there is actually a simpler way: $line = preg_replace ('/,[\d]+\-[\d]+/i ', ' ', $line);

There is a word, forget the others said or their own original, anyway in my mind for a long time: the regular expression is not technology, but the skills: the regular introduction is actually quite simple, with one or two times to master the basic grammar, to let this sword play out how much power, to see the personal practice. One point to note is that the sword is actually a double-edged sword: the regular execution efficiency is actually lower than that of the strstr,strpos, so when your search is simple, there is no need to use the regular.

  • 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.