Principles and personal experience of Regular Expressions

Source: Internet
Author: User

Regular Expressions are very common tools for programmers. It is very efficient and convenient to search and extract data, so it is necessary to have a deeper understanding of them.

When using regular expressions, you must pay attention to its automatic mechanism. Specifically, each node only pays attention to the state of the next input. There is a classic trap. Use the regular expression *? B matches the aab. If you look at it, you must think that the matching result will be AB. Actually, it is not. He first matches the first a and then determines whether the next B is used. If not, then he checks whether it is a or not. Therefore, the result is aab.

Regular expressions have two implementations: NFA and DFA. In comparison, DFA is more efficient, while NFA is simpler and occupies less space. No matter what implementation, there is actually no difference in functionality, but it should be noted that the specific implementation of different regular expressions may be different, for example, the regular expressions written in java are not necessarily applicable in grep, but they are complicated and may cause problems. For more information, see the previous article http://passover.blog.51cto.com/2431658/425536.

In addition, I have several experiences in use:

1. You can use [\ s \ S] to match any character. This is a common habit, although you can write many other regular expressions with the same function.

2. view the features before and after matching results using the surround view. This is often used in extraction without group. It is best to know how to use it.

3. Remove unnecessary groups. This problem is often encountered when writing a fixed group value. The brackets have to be used for the previous matching, so that a group is more than expected (? : Xxx) to declare that this bracket is not a group.

4. performance problems. In fact, I personally do not pay too much attention to the optimization of regular expressions, but it is important to note that we should not use greedy nesting. This performance is very low.

5. Use the environment with the same regular expression to verify the regular expression. For example, a java program needs to call the Regular Expression and write a java test program to verify the regular expression. I have worked on a GUI tool for simple java Regular Expression verification. It has been quite easy to use for many years. I will share it with you in the attachment.

In general, do not use regular expressions. The key is to master the principles and use them flexibly. I hope everyone can use regular expressions with ease.

The last page that comes with a common regular expression is http://passover.blog.51cto.com/2436658/425533. Chinese matching is quite useful.

 

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.