Linux Regular Expressions-write regular expressions

Source: Internet
Author: User

For all practical purposes, you can produce the correct results by using the program. However, it does not mean that the program is always working as correctly as you would like it to be. In most cases, if the program does not produce the desired output, it can be concluded that the real problem (excluding input or grammatical errors) is how to describe what you want.

In other words, the place where you should consider correcting the problem is the expression that describes the desired result. The expression is incomplete or the formula is not represented correctly. For example, if the program evaluates the following expression:
Pay = Weekly_salary * 52

And know the values of these variables, it will calculate the correct results. But some people would object, because the formula did not explain the salesperson, he also got a commission. So to describe this situation, the expression needs to be re-represented as:
Pay = Weekly_salary * + COMMISSION

You might think that the person who wrote the first expression did not fully understand the scope of the problem and therefore could not describe it well. It is important to know how to describe it in detail. If you ask someone to take a book for you, and if you have a lot of books in front of you, then you need to be more specific about the book you want (or the process of choosing to be satisfied with the uncertainty).

The same applies to the hermetical expression. Programs (such as grep) are simple and easy to use. It is not difficult to understand the elements of regular expressions. Regular expressions allow you to write simple or complex schema descriptions. The difficult (and interesting) factor in writing regular expressions is the complexity of the application: patterns appear in a variety of situations and contexts. Complexity is inherent in the language itself, just as you do not always get a correct understanding of the semantics by querying a dictionary.

The process of writing a regular expression involves 3 steps:
1. Know what to match and how it appears in the text.
2. Write a pattern that describes what to match.
3. Test the mode to see what it matches.

Testing the matching description will ensure that the description is the same as expected. It usually reveals some amazing things. Careful examination of the results of the tests, comparison of output and input, can greatly improve the understanding of regular expressions. The results of pattern matching can be resolved as follows:
Hits (HIT)
-This is the line I want to match.
Misses (not hit)
-This is the line I don't want to match.
Omissions (omitted)
--This is the line that I can't match but want to match.
False alarms (false alarm)
-This is the line that I don't want to match but match.

The description of the attempt to perfect the pattern can also be resolved from the opposing segment, and you can try to trap the omission by limiting the possible matches to the wrong false alarms, usually by extending the possible matches.

The difficulty is especially noticeable when you have to use a fixed string description pattern. Removing each character from the pattern of a fixed string increases the number of possible matches. For example, when searching for the string "What" and also deciding to match "what", then the only fixed string pattern matching "what" and "what" is "hat", which is the longest string common to both. Obviously, the search for "hat" will produce an unwanted match. Adding each character to a fixed string pattern can reduce the number of matches. The string "them" usually produces fewer matches than the string "the".

Reference: http://www.linuxawk.com/communication/409.html

Linux Regular Expressions-write regular expressions

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.