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

Example Analysis of regular expressions in linux

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article will explain in detail the example analysis of regular expressions in linux. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Regular expressions are widely used, such as php,Python,java, etc., but the most commonly used regular expression commands in linux are grep (egrep), sed,awk, etc., in other words, linux three musketeers must be inseparable from regular expressions if they want to work more efficiently.

1. What is a regular expression?

In a nutshell, a regular expression is a set of rules and methods defined to deal with a large number of strings. With the help of these special symbols defined, the system administrator can quickly filter, replace, or output the required strings. Linux regular expressions are generally handled in behavioral units.

2. Why learn regular expressions

In the enterprise work, in the linux operation and maintenance work we do every day, we are always faced with a large number of text configuration, programs, command output and log files with strings, and we often have an urgent need to find specific strings that meet the needs of our work from a large number of string contents, which depends on regular expressions, so it can be said that regular expressions are generated to filter such strings!

3. Two points for attention that are easy to be confused:

1) linux regular expressions are generally handled in behavioral units.

2) regular expressions are essentially different from our commonly used wildcard special characters, such as: ls *. Txt where * is a wildcard character (for all), not a regular expression.

Pay attention to character set problems:

Make sure the character set: export LC_ALL=C

-

Basic regular expression + extended regular expression meaning explanation:

-

. Represents and can only represent any one character (excluding blank lines)

* repeat any of the first 0 or more characters

. * matches all characters. (including blank lines)

Sed-ri's # (. *) #\ 1roomg 'bqh.txt

Use\ 1 to take out the results in parentheses of the previous regular match.

^ indicates what to start with, and ^ bqh starts with bqh

What does $end with?

^ $represents a blank line.

\ example. It just represents the dot itself, escaping symbols, allowing characters with a special identity to move, take off the vest, and restore the prototype\ $

^. * starts with any number of characters.

.* $ends with any number of characters.

(. *) from the first character match to the space stop

[abc] matches any character in the character set [a-zA-Z]

[^ abc] matches the content of any character that does not include ^; the ^ in square brackets is reversed, note and use. The difference at the beginning.

A\ {n ~ m\} repeat n to m times, the previous repeated character. If you use egrep/sed-r, you can remove the slash.

Repeat at least n times, the previous repeated character. If you use egrep/sed-r, you can remove the slash.

\ {n\} repeat n times, the previous repeated character. If you use egrep/sed-r, you can remove the slash.

① ^ word search for a line that begins with word; vi ^ is enough

② word$ search for the beginning of a line ending with word; vi $

③ ^ $represents a blank line.

Extended regular expression: ERP (egrep or grep-E)

+ repeat one or more preceding characters

? A character before 0 or a 0

| | use or to find multiple matching strings |

() find the "user group" string

Examples of actual combat:

^ m searches for objects that start with m

P $search for p-terminated

^ $indicates an empty number

Remove the blank line: grep-v "^ $" bqh.log

Check the removed blank line content: grep-vn "^ $" bqh.log

. Represents and can only represent any one character (excluding blank lines)

Look for characters with 0:

. * matches all characters. (including blank lines)

Look for. The character at the end:

Wrong method: grep ". $" bqh.log

The correct method:

Grep "\. $" bqh.log

Note:\. It just represents the dot itself, escaping symbols, allowing characters with a special identity to move, take off the vest, and restore the prototype\ $

* example 1 * repeat one or more of the preceding characters.

Grep-o "1*" bqh.log / /-o exact match

^. * starts with any number of characters.

.* $ends with any number of characters.

[abc] matches any character in the character set [a-zA-Z]

Matches any lowercase character of amurz in the character set:

[^ abc] matches the content of any character that does not include ^; the ^ in square brackets is reversed, note and use. Difference at the beginning

Match any character that is not a number:

A\ {n ~ m\} repeat n to m times, the previous repeated character. If you use egrep/sed-r / grep-E, you can remove the slash.

Repeat at least n times, the previous repeated character. If you use egrep/sed-r, you can remove the slash.

\ {n\} repeat n times, the previous repeated character. If you use egrep/sed-r, you can remove the slash.

Note: egrep,grep-E or sed-r filter general special characters can not be escaped. Use more parameters.

Extended regular expression: ERP (egrep or grep-E)

+ repeat one or more preceding characters

? A character before 0 or a 0

| | use or to find multiple matching strings |

() find the "user group" string

This is the end of this article on "sample analysis of regular expressions in linux". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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