Git Product home page Git Product logo

Comments (8)

thori0908 avatar thori0908 commented on August 11, 2024

■課題1

  • 数字のみの値:

$ grep ^[0-9][0-9]*$ issue9

  • 英字のみの値:

$ grep ^[a-z][a-z]*$ issue9

  • 英数字のみの値:

$ grep ^[0-9a-zA-Z][0-9a-zA-Z]*$ issue9

  • 先頭が123で始まる値:

$ grep ^123 issue9

  • 末尾が0で終わる値:

$ grep 0$ issue9

ref.
http://itpro.nikkeibp.co.jp/article/COLUMN/20060228/231171/
http://h50146.www5.hp.com/products/software/oe/hpux/developer/column/unixtext_app01/02.html

■課題2

  • 先頭が\で始まる値:

$ grep "^[\]" issue9

  • 5桁の数字のみの値:

$ grep "^[0-9][0-9][0-9][0-9][0-9]$" issue9

  • 末尾がeもしくはmで終わる値:

$ grep "m$\|e$" issue9

■課題3

  • 電話番号:

$ grep -EHn ^[0-9]+-[0-9]+-[0-9]+$ issue9

  • メールアドレス([英数字記号]@[英数字記号]程度の正規表現):

$ grep [0-9a-zA-Z]@[0-9a-zA-Z] issue9

ref.
http://write-remember.com/program/%EF%BD%92egular_expression/

from training.

fr-matsuo avatar fr-matsuo commented on August 11, 2024

確認しました!
いくつか楽に書ける部分があるので、下記を使って書き直してみてくださいー

課題1
  • 数字のみの値
  • 英字のみの値
  • 英数字のみの値

直前の文字の一回以上の繰り返しを表すメタ文字(一度使っているようです)

課題2
  • 5桁の数字のみの値

直前の文字をn回繰り返すという表現

from training.

fr-itaya avatar fr-itaya commented on August 11, 2024

確認しましたー。

課題3

英数字だけでなく、記号を表す正規表現も追加してみましょう!

from training.

thori0908 avatar thori0908 commented on August 11, 2024

■課題1

  • 数字のみの値:

$ grep "^[0-9]\+$" issue9

  • 英字のみの値:

$ grep "^[a-z]\+$" issue9

  • 英数字のみの値:

$ grep "^[0-9a-zA-Z]\+$" issue9

  • 先頭が123で始まる値:

$ grep ^123 issue9

  • 末尾が0で終わる値:

$ grep 0$ issue9

ref.
http://itpro.nikkeibp.co.jp/article/COLUMN/20060228/231171/
http://h50146.www5.hp.com/products/software/oe/hpux/developer/column/unixtext_app01/02.html

■課題2

  • 先頭が\で始まる値:

$ grep "^[\]" issue9

  • 5桁の数字のみの値:

$ grep "^[0-9]\{5\}$" issue9

  • 末尾がeもしくはmで終わる値:

$ grep "m$\|e$" issue9

■課題3

  • 電話番号:

$ grep -EHn ^[0-9]+-[0-9]+-[0-9]+$ issue9

  • メールアドレス([英数字記号]@[英数字記号]程度の正規表現):

$ grep "[0-9a-zA-Z:punct:]@[0-9a-zA-Z:punct:]" issue9

ref.
http://write-remember.com/program/%EF%BD%92egular_expression/

from training.

fr-itaya avatar fr-itaya commented on August 11, 2024

確認しました!OKです!
:punct:という文字クラスがあったとは:+1:

from training.

fr-matsuo avatar fr-matsuo commented on August 11, 2024

確認しました、OKです!

from training.

fr-matsuo avatar fr-matsuo commented on August 11, 2024

" "について

" "は、**shellの特殊文字を特殊文字として扱わない(エスケープ)**ためのものです。
下記は、shell上で引数の区切りを表す特殊文字である、半角スペースの例です。


shellコマンドは一度shellで解釈された後に実行されます。

grep "^a b" file

とすると、shellが

grep(実行コマンド)  "^a b"(引数1)  file(引数2)

と解釈して、grepに引数1・2を渡して呼び、grepが

grep  "^a b"(パターン)  file(ファイル名)

と解釈します。


一方、

grep ^a b file

と入力すると、shellが

grep(実行コマンド)  ^a(引数1)  b(引数2) file(引数3)

と解釈して、grepに引数1~3を渡して呼び、grepが

grep ^a(パターン)  b(ファイル名1)  file(ファイル名2)

と解釈します。


;などもエスケープされますが、\などの特殊文字は' 'でないとエスケープされません。

from training.

fr-itaya avatar fr-itaya commented on August 11, 2024

$ grep "pattern" file
$ grep pattern file

上記2つのコマンドを入力すると,基本的に同じ結果が出力されますが,
" "の違いは何なんでしょう?

補足します!
" "の有無による違いは次の場合に出てきます。

1. patternに空白を含む場合

grepコマンドの取る引数を確認して頂きたいのですが、" "無しで空白を含むpatternを検索すると、空白より後の文字列がファイル名として認識されてしまいます。

2. patternにワイルドカードを文字列として含む場合

ワイルドカードがShellによって展開されてしまい、意図しない文字列がマッチしてしまいます。
(ワイルドカードにどのような記号が該当するかは、調べてみて下さい!)

以上の事象を防ぐために、ダブルクオーテーション""もしくはシングルクオーテーション''で囲っておく必要があるのですー。

ref.
http://h50146.www5.hp.com/products/software/oe/hpux/developer/column/unixtext_app01/

下記を試してみて下さい!(追加課題ではありません)
1 → txtファイルの"12345"を"123 45"に書換え、$grep 123 45 issue9$grep "123 45" issue9
2 → txtファイルの"12345"を"123_45"に書換え、$grep "123\_45" issue9$grep 123*45 issue9
それぞれ違う結果になると思います。

from training.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.