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

A probe into the door path of grep and egrep regular expressions

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

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

What is a regular expression?

Regular expression, also known as regular representation, conventional representation (Regular Expression, often abbreviated as regex, regexp or RE in code), is a pattern written by a class of characters, many of which do not express their literal meaning, but express functions such as control or wildcard. Regular expressions use a single string to describe and match a series of strings that conform to a syntactic rule. In many text editors, regular expressions are often used to retrieve and replace text that matches a certain pattern. Many programming languages support string manipulation using regular expressions.

The regular expression language consists of two basic character types: literal (normal) text characters and metacharacters. Metacharacters make regular expressions capable of processing. The so-called metacharacters refer to those special characters with special meaning in regular expressions, which do not represent their literal meaning, but are used for additional functional description.

Regular expression classification

Regular expressions can be divided into basic regular expressions (grep) and extended regular expressions (egrep) according to the number and function of metacharacters. Grep and egrep are both text search tools, which can search the target file line by line according to the user-specified text pattern (search criteria) to display the lines that can be matched. Grep-E is equivalent to egrep in usage, and fgrep, a text search tool similar to grep and egrep, is not introduced separately because it does not support regular expressions.

Basic regular expression (grep)

Syntax format: GREP [option]... 'PATTERN' FILE...

Basic regular expression metacharacters:

1. Character matching:

.: any single character

[]: matches any single character in the specified range

[0-9]: matches a single number

[amurz]: matches a single lowercase letter

[Amurz]: matches individual uppercase letters

[[: digit:]]: matches a single number

[[: lower:]]: matches individual lowercase letters

[[: upper:]]: matches individual uppercase letters

[[: space:]]: matches a single white space character

[[: punct:]]: matches a single punctuation mark

[[: alnum:]]: matches a single letter or number

[[: alpha:]]: matches a single letter (regardless of case)

[^]: matches any single character outside the range

two。 Number of times match:

*: any time (any number of times the preceding character appears)

\?: 0 or 1 time (preceding characters are optional)

\ {m\}: M times (previous characters appear m times)

At least m times and no more than n times

\ {m,\}: at least m times

\ {0magnetic n\}: n times at most

. *: any character of any length

3. Position Anchor:

^: anchor at the beginning of the line (written on the far left of the pattern)

$: anchored at the end of the line (written on the far right of the pattern)

^ $: blank line

\, / b: suffix anchoring (appears on the right side of the word)

(note: a string called word consisting of consecutive characters that do not contain special characters)

4. Grouping

\ (\): such as\ (ab\) *

(the content matched by the patterns in the grouping can be remembered in memory by the regular expression engine and then referenced.)

5. Quote

\ n: reference the content matched by the nth left parenthesis and the corresponding pattern in the right parenthesis

Extended regular expression (egrep)

Syntax format: egrep [option] 'PATTERN' FILE...

Extend regular expression metacharacters:

1. Character matching:

.: any single character

[]: match any single character in the range

[^]: matches any single character outside the range

two。 Number of times match:

*: any time

0 or 1 time

+: at least once

{m}: M times

{mdirection n}: at least m times, at most n times

{m,}: at least m times

{0magnetic n}: n times at most

3. Position Anchor:

^: anchor at the beginning of the line

$: anchor at the end of the line

\,\ b: suffix anchoring

^ $, ^ [[: space:]] * $: blank line

4. Grouping:

(): grouping

|: or

5. Reference:

\ n: reference the content matched by the nth left parenthesis and the corresponding pattern in the right parenthesis

Grep and egrep command options

-v: reverse selection

-o: only the matching string is displayed, not the line on which the string is located

-i:ignore-case, ignoring character case

-A: display the next two lines

-B: show the first two lines

-C: display the upper and lower lines

And try it out

Show lines in the / proc/meminfo file that begin with uppercase or lowercase S.

Show users in the / etc/passwd file whose default shell is not / sbin/nologin

Shows the user with the largest ID number among the users whose default shell is / bin/bash in the / etc/passwd file

Find one or two digits in the / etc/passwd file

Displays lines in / boot/grub/grub.conf that begin with at least one white space character

Displays a line in the / etc/rc.d/rc.sysinit file that begins with #, followed by at least one white space character, followed by at least one non-white space character

Find the line ending in 'LISTEN' in the execution result of the netstat-tan command

Find the user on the current system whose user name is the same as the default shell

Displays the default shell for root or scholar users on the current system

Find a word in the / etc/rc.d/init.d/functions file followed by a set of parentheses "()" lines

Use the echo command to output a path, and then use grep to extract its base name

Find the number between 1 and 255in the result of the ifconfig command

The End

Okay, to sum up here, the commonly used regular expression usage is almost sorted out, the above is a summary of personal learning, if there are mistakes and omissions, large

God don't spray.

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

Network Security

Wechat

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

12
Report