ホームページ  >  記事  >  ウェブフロントエンド  >  Linux で正規表現を使用するためのヒント

Linux で正規表現を使用するためのヒント

php中世界最好的语言
php中世界最好的语言オリジナル
2018-06-09 14:34:551130ブラウズ

今回は、Linux で正規表現を使用する際の注意点についてお届けします。実際の事例を見てみましょう。

1. 構成

一般的な文字: 通常の文字列、特別な意味なし
特殊文字: 正規表現で特別な意味を持つ
正規表現での一般的なメタ文字 [特殊文字]

2. POSIX のメタ文字BRE [基本] および ERE [拡張]

: 通常、(...) などの後続の文字の特別な意味を開くまたは閉じるために使用されます [は、記号が削除されたエスケープ文字です 特別な意味、()、 {} などはシェル内で特別な意味を持ちます]
と and の違い:

[root@localhost ~]# cat -n test.txt
1 gd
2 god
3
4 good
5 good
6 goad
7
8 gboad

2.1, .: 任意の 1 文字と一致します (空にすることはできない null を除く)

[root@localhost ~]# grep - n ". " test.txt "
1:gd
2:god
4:good
5:good
6:goad
8:gboad
[root@localhost ~]# grep -n "go.d" test.txt
4:good
6:goad

2.2, : o など、前の文字と何度でも一致します。o は、o なしでも、o が 1 つでも、複数でも構いません

[root@localhost ~]# grep -n " *" test。 txt
[root@localhost ~]# grep -n "o*" test.txt
1:gd
2:god
3:
4:good
5:good
6:goad
7:
8:gboad
[root@localhost ~]# echo "gbad" >>test.txt
[root@localhost ~]# echo "pbad" >>test.txt
[root@localhost ~]# echo "kgbad" > ;>test.txt
[root@localhost ~]# echo "poad" >>test.txt
[root@localhost ~]# grep -n "go*" test.txt [o OK いいえ、g o の前に一致する必要があります]
1:gd
2:god
4:good
5:good
6:goad
8:gboad
9:gbad
11:kgbad

*2.3,.すべて一致)、空にすることもできます **

[root@localhost ~]# grep -n ".*" test.txt
1:gd
2:god
3:
4:good
5:good
6 :goad
7:
8:gboad
9:gbad
10:pbad
11:kgbad
12:poad
[root@localhost ~]# grep -n "go.*" test.txt
2:god
4 :good
5:good
6:goad
[root@localhost ~]# grep -n "po.*" test.txt
12:poad
[root@localhost ~]# echo "pgoad" >>test .txt
[root@localhost ~]# grep -n "go.*" test.txt [go に一致した後に任意の文字が存在し、空にすることもできます]
2:god
4:good
5:good
6:goad
13:pgoad
[root@localhost ~]#
[root@localhost ~]# grep -n "o.*" test.txt
2:god
4:good
5:good
6: goad
8:gboad
12:poad

2.4, ^:...で始まる次の正規表現と一致します

[root@localhost tmp]# grep "^root" /etc/ passwd
root:x:0:0 :root:/root:/bin/bash
[root@localhost tmp]#

2.5, $:...

[root@localhost tmp]# grep で終わる直前の正規表現と一致します。 "bash$" /etc/passwd | head -1
root:x:0:0:root:/root:/bin/bash
[root@localhost tmp]#
^$: 空白行を意味します
"#| ^$": #

2.6 で始まるコメント行と空白行に一致します。 []: 角括弧内の任意の文字

([ sS] など、s に一致、または S に一致) に一致します。ハイフン (-)ハイフンの範囲を指定するために使用できます ([(0-9)] など、0 ~ 9 の任意の文字に一致します)。角括弧の最初の位置にある ^ 記号が一致を意味する場合は、[^0-9]リストにない文字。

[root@localhost tmp]# 猫ホスト
192.168.200.1
192.168.200.3
a.b.123.5
23.c.56.1
1456.1.2.4
12.4.5.6.8
[root@localhost tmp]# grep - E '([0-9]{1,3}.){3}[0-9]{1,3}' ホスト
192.168.200.1
192.168.200.3
1456.1.2.4
12.4.5.6.8
[ root@localhost tmp]# grep -E '^([0-9]{1,3}.){3}[0-9]{1,3}$' ホスト
192.168.200.1
192.168.200.3
[ root@localhost tmp]#

2.7, ?: 前の文字と 0 回以上一致します

[root@localhost ~]# grep -E "go?d" test.txt
gd
god
[ root@localhost ~]#
[root@localhost tmp]# cat test
do
does
doxy
[root@localhost tmp]# grep -E "do(es)?" @localhost tmp]#




3. POSIX BRE (Basic Regular) でのみ見つかる文字

{n,m}: 間隔式、繰り返す前の 1 文字と一致します [繰り返し、https{0,1} などの次の 1 文字、つまり s を 0 ~ 1 回繰り返します。 {n} は n 回のマッチングを意味し、{n,m} は n ~ m 回のマッチングを意味し、{n,} は少なくとも n 回のマッチングを意味し、{,m} は最大 m 回のマッチングを意味します。 [エスケープ文字]

4、POSIX ERE (Extended Regular) のみの文字

4.1、{n,m}: BRE の {n,m} と同じ機能

[root@ localhost tmp] # grep -E '^([0-9]{1,3}.){3}[0-9]{1,3}$' ホスト
192.168.200.1
192.168.200.3

4.2, + : 前の正規表現の 1 つ以上と一致します

[root@localhost ~]# egrep "go+d" test.txt
god
good
good
[root@localhost ~]#

4.3, | : 複数の文字列 [または関係] の一致を示します

[root@localhost ~]# grep -E "3306|1521" /etc/services
mysql 3306/tcp #MySQL
mysql 3306/udpライセンスマネージャー
[root@ localhost ~]#


4.4, ( ): グループフィルタリング、後方参照

グループフィルタリング


[root@localhost ~]# echo "glad" >>
[root@localhost ~] ]# egrep "(la|oo)" test.txt

good
good
Glad


() 後方参照; 前の一致部分が括弧を使用している場合、最初の括弧の内容は後の括弧で 1 として出力できます。部分など。


[root@localhost tmp]# ifconfig |sed -rn 's#.*addr:(.*)(B.*)$#1#gp'
192.168.4.27



5.式のメタキャラクター


5.1、b: 単語の境界に一致する

[root@localhost tmp]# cat test
do

does
doxy
agdoeg
[root@localhost tmp]# grep "dob " test
do
[root@localhost tmp]# grep "bdo" テスト
do
does
doxy
[root@localhost tmp]# grep "bdoes" テスト
does
[root@localhost tmp]# grep "bdob" テスト
do
[root@localhost tmp]#


5.2. B: b

[root@localhost tmp]# grep "doB" test
does

doxy
agdoeg
[root@localhost] の反対の非単語境界に一致します。 tmp]# grep "dob" test
do
[root@localhost tmp]#


5.3、d: [0-9] に相当する数字と一致します

5.4、D: 数字以外の文字と一致します、[^0-9]

5.5 に相当、w: 文字、数字、アンダースコアに一致、[A-Za-z0-9_] に相当ケース: ブートの効率化

[root@localhost ~]# chkconfig --list| egrep -v "crond|network|rsyslog|sshd|sysstat" | awk '{print "chkconfig", $1,"off"}' |bash


この記事の事例を読んだ後は、この方法を習得したと思います。さらに興味深い情報については、PHP 中国語 Web サイトの他の関連記事に注目してください。

推奨読書


Angularプロジェクトでscssを操作する方法

axios 302ステータスコード

以上がLinux で正規表現を使用するためのヒントの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。