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

Regular expressions and grep

2025-04-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Regular expressions are divided into two categories: basic regular expressions and extended regular expressions.

Basic regular expression metacharacters:

Character matching:

.: matches any character.

[]: matches any character in the specified range in [].

[^]: any character outside the specified range; you can also know that in [] parentheses, ^ indicates inversion.

Number of times match:

*: the character before it appears any number of times.

\?: the preceding character appears 0 or 1 times

\ {mdirection n}: the first character appears at least m times and up to n times. If there is no upper limit, n can not be written, and if it occurs at least 0 times, m is 0. M cannot be omitted.

. *: any character of any length appears before it.

Character Mou Ding:

^: the head of the line is Mou Ding.

&: Mou Ding at the end of the line.

\

< 或 \b:牟定单词在行首 \>

Or\ b: the word Mouding is at the end of the line

\ (\): used for grouping. Backward reference:\ 1,\ 2,\ 3. Indicates the contents in parentheses of the nth pair of references

Grep: commands that use patterns defined by basic regular expressions to filter text are supported.

-I: ignore the case of matching letters

-v: reverse matching, displayed as matching content

-o: only the matching content is displayed

-- color: color display

-E=eprep: use extended regularities for matching.

-A digits,-B digits,-C digits:-A (after) shows not only the matching lines, but also the next N rows that match. -B (before) indicates that the first N lines of the match are displayed at the same time, and-C indicates that the first N lines and the last N lines are displayed at the same time.

More options are viewed in man.

To be clear, basic regular expressions work in greedy mode.

Extended regular expression

Character matching, position determined: same as basic regular

.

[]

[^]

Number of times match:

*: the same as the basic regular, the characters preceding it appear any number of times.

? Same as the basic regular, the preceding character appears 0 or 1 times

+: an extra + matches its first character at least once. Equivalent to\ {1,\}? + = *

{mdirection n}: you don't need to use\ to change your mind in the extension rule.

Grouping and

(): grouping

1, 2, 3. Backward reference

Or

|: | the front and rear are each as a whole. For example, abc | dec: indicates abc or dec. You can use egrep-- color for verification. To indicate c or d but want (), ab (c | d) ec

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

Servers

Wechat

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

12
Report