Comments (8)
■課題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.
確認しました!
いくつか楽に書ける部分があるので、下記を使って書き直してみてくださいー
課題1
- 数字のみの値
- 英字のみの値
- 英数字のみの値
直前の文字の一回以上の繰り返しを表すメタ文字(一度使っているようです)
課題2
- 5桁の数字のみの値
直前の文字をn回繰り返すという表現
from training.
確認しましたー。
課題3
英数字だけでなく、記号を表す正規表現も追加してみましょう!
from training.
■課題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.
確認しました!OKです!
:punct:
という文字クラスがあったとは:+1:
from training.
確認しました、OKです!
from training.
" "
について
" "
は、**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.
$ 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)
- サーバサイド - フレームワーク - フレームワークとは HOT 3
- サーバサイド - フレームワーク - Laravel - インストールと設定 HOT 3
- サーバサイド - フレームワーク - Laravel - routingとview作成
- サーバサイド - フレームワーク - Laravel - テンプレートエンジンのBladeを使う HOT 2
- サーバサイド - フレームワーク - Laravel - Controller作成 HOT 1
- サーバサイド - フレームワーク - Laravel - フォームのバリデーション、CSRF対策 HOT 2
- フロントエンド - JavaScript/jQuery - JavaScript/jQueryを理解する HOT 3
- フロントエンド - JavaScript/jQuery - Hello World!
- フロントエンド - JavaScript/jQuery - 関数を作る
- フロントエンド - JavaScript/jQuery - DOM操作
- フロントエンド - JavaScript/jQuery - イベント操作
- フロントエンド - JavaScript/jQuery - 条件分岐
- フロントエンド - JavaScript/jQuery - ajax通信
- サーバサイド - サーバサイドテスト - 自動ユニットテストの基礎 HOT 3
- サーバサイド - PHP - PHPを理解する HOT 6
- サーバサイド - PHP - PHPを書く HOT 11
- サーバサイド - PHP - フォーム作成
- フロントエンド - JavaScriptTraining HOT 1
- ruby - 環境構築 HOT 8
- ruby - 基本構文 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from training.