In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Overview of regular expressions
Use a single character to describe and match a series of strings that conform to the rules of a sentence, which is composed of ordinary characters and special characters. Regular expressions are widely used in script programming and text editors.
Regular expressions are abbreviated to regex, regexp, RE.
In most languages, regular expressions are included in two forward slashes.
Regular expressions have a feature of "greedy matching": try to match the longest.
Regular expression classification basis regular expression
\ escape character. For example: "!" The logic is no! As ordinary characters,\ can also wrap the line example 1: [root@localhost ~] # ls-l / home\ > / tmp example 2: [root@localhost ~] # grep "b.*in" a.txt 12 bin 3423 sb.*in 2344 binary 4923 binlog 23 [root@localhost ~] # grep "b\.\ * in" a.txt 23 sb.*in 23
^ matches the position where the string begins, with … At the beginning
Match the position where the string ends with … For the end of
^ $represents a blank line, not a space
Example: [root@localhost ~] # cat a.txt aaaa bb aaa b a [root@localhost ~] # grep "^ aa$" a.txt aa. Match any string * match preceding subexpression (character) 0 or more examples: [root@localhost ~] # grep "a 0*" a.txt aa 0aa 00aa bb 0000 aa 00a b a 000.* match any string [list] matches a character in the list list For example: [abc], [amurz], [a-zA-Z0-9] example: [root@localhost ~] # ifconfig ens32 | grep "TX [pe]" TX packets 1776 bytes 206946 (202.0 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [^ list] matches a character in any non-list table For example: [^ amurz], [^ 0-9], [^ A-Z0-9] example: [root@localhost ~] # ifconfig ens32 | grep "TX [a murf]" TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [root@localhost ~] # ifconfig ens32 | grep "TX [^ a Mustf]" TX packets 1814 bytes 210662 (205.7 KiB)\ {n\} match previous subexpression n times example: [root@localhost ~] # grep "a 0\ {2\}" a.txt Aa 00aa bb 0000 aa 00a b a 000\ {n \} match front subexpression at least n times [root@localhost ~] # grep "a 0\ {2,\}" a.txt aa 00aa bb 0000 aa 00a b a 000\ {n department m\} match front subexpression at least n times Maximum m times\
< 词首牟定:固定词首\>Word undefined: fixed suffix example: [root@localhost ~] # grep "\" a.txt a b a 000escape character meaning\ a ring (BEL)\ b backspace (BS) Move the current position to the previous column\ nnewline (LF) move the current position to the beginning of the next line\ r enter (CR) move the current position to the beginning of the line\ t horizontal tabulation (HT) to the next TAB position\ v Vertical tabulation (VT)\ represents a backslash character "\"
Extended regular expression
+ match the previous sub-expression for 1 or more times
? Match the previous sub-expression 0 or 1 times
() take the string in parentheses as a whole
| | matches characters by or |
Linux commonly used text processing tools grep matching, query (filtering) sed editing (add, delete, modify) awk text formatting (string extraction) Note Linux regularities generally deal with files in units of behavior alias grep='grep-- color=auto' Note character set, LANG=C
Wildcard character
Regular expressions are fundamentally different from the wildcards we use on the command line.
Wildcards usually deal with file names
Ls-l.txt
Any string of any length, belonging to wildcards
?: a single arbitrary string that belongs to a wildcard.
The parameters of the grep command:
-v exclude matching content (inverted)
-E supports extended regular expression = egrep
[root@localhost ~] # grep-Ev "^ $| #" / etc/httpd/conf/httpd.conf ServerRoot "/ etc/httpd" Listen 80Include conf.modules.d/*.conf
-I ignore case
[root@localhost] # cat a.txtaAAA [root@localhost] # grep-I "a" a.txtaAAA [root@localhost ~] #
-o output only matching content
[root@localhost ~] # grep-o "root" / etc/passwdrootroot
-display color of content matched by color=auto (not much alias view aliases are used)
-n displays the line number at the beginning of the line
[root@localhost ~] # grep-n "root" / etc/passwd1:root:x:0:0:root:/root:/bin/bash10:operator:x:11:0:operator:/root:/sbin/nologin43:admin:x:1000:1000:root:/home/admin:/bin/bash
-Q matching content is not displayed (silent output, usually used when writing scripts and not output to the screen)
-w match the filter criteria as words
[root@localhost ~] # grep-w "bin" / etc/passwdroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologinsync:x:5:0:sync:/sbin:/bin/syncadmin:x:1000:1000:root:/home/admin:/bin/bash [root@localhost ~] # grep "bin" / etc/passwdroot:x:0:0:root:/root:/bin/bashbin:x:1 : 1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologin
-c outputs only the count of matching rows
[root@localhost ~] # grep-c "root" / etc/passwd3
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.