Hone the skills of building regular expression patterns-regular expressions for system management

Source: Internet
Author: User
Tags command line regular expression egrep

Introduction: Through this study, you can add some useful skills for designing actual regular Expressions (regexp). Building regular expressions is part of any administrator's daily work. In order to construct a successful regular expression that returns the required conditions, you need to learn to think in a pattern-matching perspective, which takes a lot of time to practice.

Introduction

UNIX® administrators need to build and use regular expressions (regexp) for text pattern matching on a daily basis. Most languages support some kind of implementation of regular expressions. Some applications, such as EMACS, have regular expression search capabilities, and you can use regular expressions with various command-line tools. Whatever the application, the key to building the correct regular expression is to identify patterns that satisfy only the data that needs to be matched in order to exclude other unnecessary content in the input.

For this purpose, this article will step through several regular expression pattern building techniques and explain how they can help you with a variety of common tasks.

Use regular Expressions (regexp)

Unless specifically stated, the examples used in this article are regular expressions that extend the portable operating system interface (extended POSIX). If you use them through the command line (such as using the Egrep utility), you should refer to the various regular expressions as needed. Keep in mind that there are some differences between the different regular expression implementations, and you may have to adapt to specific implementations in the specific tools, applications, or languages you use.

Match whole line of content

The ^ metacharacters match the beginning of the line, while the $ matches the end of the row, and if they are grouped together (such as ^$), they match the blank row. (The mirror of this expression, $^, is unlikely to match successfully, and it will never be able to match to a valid row.) This basic regular expression is the basis for many complex regular expressions, and if you are not accustomed to using this basic regular expression, you should gradually develop the habit of using it. Use it to build a pattern that matches the entire line of content.

Searching in the user dictionary file (/usr/dict/words) is a good basic pattern. (Some versions of UNIX put user dictionaries in/usr/share/dict/words.) )

For example, suppose you forget how to spell the word fuchsia. Does it contain SH or cs? All you know is that it starts with Fu and ends with IA.

Try to search using this pattern:

$ egrep -i '^fu.*ia$' /usr/dict/words

The-I flag indicates that the search process is case-insensitive. In this example, because Fuchsia is spelled correctly, the word is included in the returned word.

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.