linuxは毎日任意の文字を練習します.繰り返し文字*
3137 ワード
[dmtsai@study ~]$ grep -n 'g..d' regular_express.txt
1:"Open Source" is a good mechanism to develop programs.
9:Oh! The soup taste good.
16:The world <Happy> is the same with "glad".
gとdの間に必ず2つの文字が存在することを強調するので、13行目のgodと14行目のgdは列挙されません!また、oo、ooo、ooooなどの資料をリストアップしたい場合は、少なくとも2つ(含む)o以上が必要ですが、どうすればいいですか?o*ですか、oo*ですか、ooo*ですか.結果を試してみてもいいですが、結果はレイアウトが多すぎます@@、だから、ここで直接説明します.
*は『0文字以上前のRE文字を繰り返す』という意味なので、『o*』は『空文字や1つ以上の文字を持つ』という意味ですが、特に空文字が許されるので、『grep-n'o*'regular_express.txt』はすべての資料をスクリーンに印刷します!
じゃあ『oo*』なら?第一のoは必ず存在しなければならず、第二のoはあってもなくてもよい複数のoであるので、o、oo、ooo、ooooooなどを含んでいれば、列挙することができる~
同様に、「少なくとも2つのo以上の文字列」が必要な場合、ooo*が必要です.つまり、
[dmtsai@study ~]$ grep -n 'ooo*' regular_express.txt1:"Open Source" is a good mechanism to develop programs.
2:apple is my favorite food.
3:Football game is not use feet only.
9:Oh! The soup taste good.
18:google is the best tools for search keyword.
19:goooooogle yes!
このように*の意味を理解しましたか?では、今練習してみましょう.文字列の先頭と末尾はgですが、2つのgの間には少なくとも1つのoしか存在しません.つまりgog、goog、goog...ちょっと待って、どうすればいいですか.
[dmtsai@study ~]$ grep -n 'goo*g' regular_express.txt18:google is the best tools for search keyword.
19:goooooogle yes!
このように理解しましたか?もう一つ、gの冒頭とgの終わりの文字列を見つけたいなら、中の文字はあってもなくてもいいですが、どうすればいいですか.『g*g』ですか?
[dmtsai@study ~]$ grep -n 'g*g' regular_express.txt1:"Open Source" is a good mechanism to develop programs.
3:Football game is not use feet only.
9:Oh! The soup taste good.
13:Oh! My god!
14:The gd software is a library for drafting programs.
16:The world <Happy> is the same with "glad".
17:I like dog.
18:google is the best tools for search keyword.
19:goooooogle yes!
20:go! go! Let's go.
しかし、テストの結果はこんなに多くの行が現れたのだろうか.おかしいでしょ?g*gの中のg*は『空字元または1つ以上のg』を表して後ろのgを加えているので、RE全体の内容はg,gg,gg,gggであるため、その行の中に1つ以上のgがあれば必要に応じている!
どうやってgを手に入れたらいいの?gの需要は?ほほほ!任意の文字を利用するああ!すなわち、「g.*g」の作法は、*が前の文字を0以上繰り返すことができるためである.『.*はゼロまたは複数の任意の文字を表す』という意味です.
[dmtsai@study ~]$ grep -n 'g.*g' regular_express.txt
1:"Open Source" is a good mechanism to develop programs.
14:The gd software is a library for drafting programs.
18:google is the best tools for search keyword.
19:goooooogle yes!
20:go! go! Let's go.
gの先頭とgの末尾を表すので、真ん中の任意の文字が受け入れられるので、1、14、20行目は受け入れられますよ.これ.*のREは任意の文字を表すのが普通で、みんなが理解して熟知することができることを望みます!もう一つ、「任意の数字」の列を見つけたいなら?数字だけなので、
[dmtsai@study ~]$ grep -n '[0-9][0-9]*' regular_express.txt
5:However, this dress is about $ 3183 dollars.
15:You are the best is mean you are the no. 1.
grep-n'[0-9]'regular_を使用しますがexpress.txtも同様の結果を得ることができますが、鳥兄は上記の命令のRE表現の意味を理解してほしいと思っています.