Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to manipulate linux regular expression grep

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the knowledge of "how to operate linux regular expression grep". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

In many technical fields (such as natural language processing, data storage, etc.), regular expressions can easily extract the information we want, so regular expressions are a very important knowledge point!

I. concept

A regular expression (Regular Expression) is a pattern used to describe a set of string characteristics and to match a specific string. Through special characters + ordinary characters to describe the pattern, so as to achieve the purpose of text matching tool.

Regular expressions are currently integrated into various text editors / text processing tools

Second, application scenarios

(1) Verification: when the form is submitted, the user name and password is verified.

(2) find: quickly extract the specified content from a large amount of information, and find the specified url in a batch of url.

(3) replacement: regular matching search is performed on the text in the specified format, and a specific replacement is made after it is found.

III. Basic elements

(1) character class

(2) quantity qualifier

(3) position qualifier

(4) Special symbols

Note: regular expressions are basically language-independent, and we can combine language / tools with regular expressions for text processing. I'll use grep for verification in a later blog. (grep is a tool for matching text by line under Linux. Here are two options that we often use.)

-E: use extended regular matching

-- color: syntactically highlight the matching content

1. Character class

Examples are as follows:

Example 1:

Note: 1.grep uses greedy matching, which matches all matches in the current line.

2.echo $? Indicates whether the match is successful (if the successful return value is 0, the unsuccessful return value is 1)

Example 2:

Example 3:

Example 4:

Example 5:

two。 Quantity qualifier

Examples are as follows:

Example 1:

Example 2:

From this, we can see that the "front unit" means by default. Or the character preceding the +

Example 3:

Example 4:

Example 5:

Example 6:

Note: the result is a matching failure and does not appear in the relevant documents. It should be an error or obsolete usage.

Example 7:

3. Position qualifier

Examples are as follows:

Example 1:

Example 2:

Example 3:

Example 4:

Example 5:

Example 6:

4. Special symbol

Examples are as follows:

What will happen if we remove the-E option?

At this point, it is not difficult to find that there is no normal regular matching after the-E option is removed, which requires us to introduce the following two concepts!

5. Basic regular expression & extended regular expression

Difference: under the extended regularization (Extended specification) and basic regularization (Basic specification) of regular expressions, some characters should be interpreted as ordinary characters, and "\" escape characters are needed to express the above special meaning. On the contrary, under the extended specification, it should be understood as a special meaning, and it should be escaped by "\" in order to take its literal value.

Therefore, the grep tool comes with the-E option, which means to use extended regularities for matching, or without this option, to use benchmark regularities for matching.

For the above questions, we give an example as follows:

Example 1:

Example 2: when the target string itself contains characters, to perform regular matching, you should do this:

Example 3:

5. Other ordinary character sets and their substitutions

For example:

To sum up, regular expressions have the following three categories:

(1) basic regular expression: Basic is BPEs

(2) extended regular expression: Extended is EREs.

(3) regular expression of Perl: PREs

Therefore, when the grep instruction does not follow any parameters, it means to use BREs, followed by "- E" to indicate the use of EREs, followed by the "- P" parameter to indicate the use of PREs.

Fourth, greedy mode and non-greedy mode

1. Greedy pattern: when a regular expression matches, it matches as much content as possible that meets the criteria

Examples are as follows:

Note: grep uses greedy matching by default, which may interfere with our test results. You can go online and use the regular online conversion tool for testing.

two。 Non-greedy pattern: when a regular expression matches, it matches as little as possible, that is, as soon as the match is found to meet the requirements, the match is successful immediately, and the match will not continue (unless there is a g, open the next set of matches)

Examples are as follows:

5. Zero width assertion

1. The so-called assertion is used to declare a fact that should be true. In regular expressions, matching continues only if the assertion is true.

two。 Zero-width assertion: like something used to find something before or after something, some special characters such as "\ b, ^, $" are used to specify a location that should meet certain conditions.

3. Classification:

(1) Zero width positive prediction advance assertion (? = exp)

It asserts the expression exp that can be matched after its occurrence. For example:\ b\ w + (? = ing\ b), which matches the first part of a word ending in ing (except ing). When we look for "I'm singing while you're dancing." It matches sing and danc

Examples are as follows:

(2) Zero width is reviewed and asserted (? > = exp)

It asserts the expression exp that can be matched in front of the position where it appears. Such as: (?

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report