grep 正規表現 逆引き

### 前提
- -E オプションを付けることで、正規表現の拡張形式（拡張正規表現）が有効になる
- 拡張正規表現で利用される文字は、? + { | ( ) である
- このドキュメントでは、拡張正規表現の文字が含まれる場合、egrep コマンドで実行している(grep -E と同じ)
- このドキュメントでは、--color=always オプションを付けて、マッチした箇所が分かるようにしている

### 利用するテキスト
```:computerScienceBooks.txt
1.      ACM/IEEE International Conference on Software Engineering
2.      Journal of Systems and Software
3.      IEEE Transactions on Software Engineering
4.      Information and Software Technology
5.      ACM SIGSOFT Iinternational Symposium on Foundations of Software Engineering
6.      empirical software engineering
7.      IEEE Software
8.      ACM SIGPLAN Conference on Programming Language Design and Implementation (PLDI)
9.      Mining Software Repositories
10.     IEEE/ACM International Conference on Automated Software Engineering (ASE)
```

### メタ文字
##### 改行文字以外の任意の1文字にマッチさせたい「.」
```bash:「.」
$ grep --color=always 'I.n' computerScienceBooks.txt
5.	ACM SIGSOFT Iinternational Symposium on Foundations of Software Engineering
```
マッチした箇所：
5.	ACM SIGSOFT <font color="Red">Iin</font>ternational Symposium on Foundations of Software Engineering

補足：Iとnの間に、必ず何らかの1文字が入らないとマッチしない。

##### 直前の文字の0回もしくは1回の繰り返しにマッチさせたい「?」
```bash:「?」
$ egrep --color=always 'I.?n' computerScienceBooks.txt
1.	ACM/IEEE International Conference on Software Engineering
4.	Information and Software Technology
5.	ACM SIGSOFT Iinternational Symposium on Foundations of Software Engineering
10.	IEEE/ACM International Conference on Automated Software Engineering (ASE)
```
マッチした箇所：
1.	ACM/IEEE <font color="Red">In</font>ternational Conference on Software Engineering
4.	<font color="Red">In</font>formation and Software Technology
5.	ACM SIGSOFT <font color="Red">Iin</font>ternational Symposium on Foundations of Software Engineering
10.	IEEE/ACM <font color="Red">In</font>ternational Conference on Automated Software Engineering (ASE)

補足：この例の正規表現は、「I」から始まり「n」で終わる任意の3文字、もしくは「In」という文字にマッチする。

##### 直前の文字の0回もしくは1回以上の繰り返しにマッチさせたい「*」
```bash:「*」
$ grep --color=always 'S.*o' computerScienceBooks.txt
1.	ACM/IEEE International Conference on Software Engineering
2.	Journal of Systems and Software
3.	IEEE Transactions on Software Engineering
4.	Information and Software Technology
5.	ACM SIGSOFT Iinternational Symposium on Foundations of Software Engineering
7.	IEEE Software
8.	ACM SIGPLAN Conference on Programming Language Design and Implementation (PLDI)
9.	Mining Software Repositories
10.	IEEE/ACM International Conference on Automated Software Engineering (ASE)
```

マッチした箇所：
1	ACM/IEEE International Conference on <font color="Red">So</font>ftware Engineering
2	Journal of <font color="Red">Systems and So</font>ftware
3	IEEE Transactions on <font color="Red">So</font>ftware Engineering
4	Information and <font color="Red">Software Technolo</font>gy
5	ACM <font color="Red">SIGSOFT Iinternational Symposium on Foundations of So</font>ftware Engineering
7	IEEE <font color="Red">So</font>ftware
8	ACM <font color="Red">SIGPLAN Conference on Programming Language Design and Implementatio</font>n (PLDI)
9	Mining <font color="Red">Software Reposito</font>ries
10	IEEE/ACM International Conference on Automated <font color="Red">So</font>ftware Engineering (ASE)

補足：この例の正規表現は、「S」から始まり「o」で終わる文字にマッチする。

##### 直前の文字の1回以上の繰り返しにマッチさせたい「+」
```bash:「+」
$ egrep --color=always 'S.+o' computerScienceBooks.txt
2.	Journal of Systems and Software
4.	Information and Software Technology
5.	ACM SIGSOFT Iinternational Symposium on Foundations of Software Engineering
8.	ACM SIGPLAN Conference on Programming Language Design and Implementation (PLDI)
9.	Mining Software Repositories
```
マッチした箇所：
2.	Journal of <font color="Red">Systems and So</font>ftware
4.	Information and <font color="Red">Software Technolo</font>gy
5.	ACM <font color="Red">SIGSOFT Iinternational Symposium on Foundations of So</font>ftware Engineering
8.	ACM <font color="Red">SIGPLAN Conference on Programming Language Design and Implementatio</font>n (PLDI)
9.	Mining <font color="Red">Software Reposito</font>ries

補足：この例の正規表現は、「S」から始まり「o」で終わる3文字以上の文字にマッチする。

### グループ
##### グループ内の何れかの文字列にマッチさせたい「()」
```bash:「()」
$ egrep --color=always '(IEEE|ACM) S' computerScienceBooks.txt
5.	ACM SIGSOFT Iinternational Symposium on Foundations of Software Engineering
7.	IEEE Software
8.	ACM SIGPLAN Conference on Programming Language Design and Implementation (PLDI)
```
マッチした箇所：
5.	<font color="Red">ACM S</font>IGSOFT Iinternational Symposium on Foundations of Software Engineering
7.	<font color="Red">IEEE S</font>oftware
8.	<font color="Red">ACM S</font>IGPLAN Conference on Programming Language Design and Implementation (PLDI)

補足：この例の正規表現は、「IEEE S」もしくは「ACM S」にマッチする。

### 文字クラス
##### 数値にマッチさせたい「-P '\d'」
```bash:「-P '\d'」
$ grep --color=always -P '\d' computerScienceBooks.txt
1.	ACM/IEEE International Conference on Software Engineering
2.	Journal of Systems and Software
3.	IEEE Transactions on Software Engineering
4.	Information and Software Technology
5.	ACM SIGSOFT Iinternational Symposium on Foundations of Software Engineering
6.	empirical software engineering
7.	IEEE Software
8.	ACM SIGPLAN Conference on Programming Language Design and Implementation (PLDI)
9.	Mining Software Repositories
10.	IEEE/ACM International Conference on Automated Software Engineering (ASE)
```
マッチした箇所：
<font color="Red">1</font>.	ACM/IEEE International Conference on Software Engineering
<font color="Red">2</font>.	Journal of Systems and Software
<font color="Red">3</font>.	IEEE Transactions on Software Engineering
<font color="Red">4</font>.	Information and Software Technology
<font color="Red">5</font>.	ACM SIGSOFT Iinternational Symposium on Foundations of Software Engineering
<font color="Red">6</font>.	empirical software engineering
<font color="Red">7</font>.	IEEE Software
<font color="Red">8</font>.	ACM SIGPLAN Conference on Programming Language Design and Implementation (PLDI)
<font color="Red">9</font>.	Mining Software Repositories
<font color="Red">10</font>.	IEEE/ACM International Conference on Automated Software Engineering (ASE)

##### 数値以外にマッチさせたい「-P '\D'」
```bash:「-P '\D'」
$ grep --color=always -P '\D' computerScienceBooks.txt
1.	ACM/IEEE International Conference on Software Engineering
2.	Journal of Systems and Software
3.	IEEE Transactions on Software Engineering
4.	Information and Software Technology
5.	ACM SIGSOFT Iinternational Symposium on Foundations of Software Engineering
6.	empirical software engineering
7.	IEEE Software
8.	ACM SIGPLAN Conference on Programming Language Design and Implementation (PLDI)
9.	Mining Software Repositories
10.	IEEE/ACM International Conference on Automated Software Engineering (ASE)
```
マッチした箇所：
1<font color="Red">.	ACM/IEEE International Conference on Software Engineering</font>
2<font color="Red">.	Journal of Systems and Software</font>
3<font color="Red">.	IEEE Transactions on Software Engineering</font>
4<font color="Red">.	Information and Software Technology</font>
5<font color="Red">.	ACM SIGSOFT Iinternational Symposium on Foundations of Software Engineering</font>
6<font color="Red">.	empirical software engineering</font>
7<font color="Red">.	IEEE Software</font>
8<font color="Red">.	ACM SIGPLAN Conference on Programming Language Design and Implementation (PLDI)</font>
9<font color="Red">.	Mining Software Repositories</font>
10<font color="Red">.	IEEE/ACM International Conference on Automated Software Engineering (ASE)</font>

##### 小文字にマッチさせたい「[:lower:]」
```bash:「[:lower:]」
$ grep --color=always 'I[[:lower:]]' computerScienceBooks.txt
1.	ACM/IEEE International Conference on Software Engineering
4.	Information and Software Technology
5.	ACM SIGSOFT Iinternational Symposium on Foundations of Software Engineering
8.	ACM SIGPLAN Conference on Programming Language Design and Implementation (PLDI)
10.	IEEE/ACM International Conference on Automated Software Engineering (ASE)
```
マッチした箇所：
1	ACM/IEEE <font color="Red">In</font>ternational Conference on Software Engineering
4 <font color="Red">In</font>formation and Software Technology
5	ACM SIGSOFT <font color="Red">Ii</font>nternational Symposium on Foundations of Software Engineering
8	ACM SIGPLAN Conference on Programming Language Design and <font color="Red">Im</font>plementation (PLDI)
10	IEEE/ACM <font color="Red">In</font>ternational Conference on Automated Software Engineering (ASE)

##### 大文字にマッチさせたい「[:upper:]」
```bash:「[:upper:]」
$ grep --color=always 'I[[:upper:]]' computerScienceBooks.txt
1.	ACM/IEEE International Conference on Software Engineering
3.	IEEE Transactions on Software Engineering
5.	ACM SIGSOFT Iinternational Symposium on Foundations of Software Engineering
7.	IEEE Software
8.	ACM SIGPLAN Conference on Programming Language Design and Implementation (PLDI)
10.	IEEE/ACM International Conference on Automated Software Engineering (ASE)
```
マッチした箇所：
1	ACM/<font color="Red">IE</font>EE International Conference on Software Engineering
3	<font color="Red">IE</font>EE Transactions on Software Engineering
5	ACM S<font color="Red">IG</font>SOFT Iinternational Symposium on Foundations of Software Engineering
7	<font color="Red">IE</font>EE Software
8	ACM S<font color="Red">IG</font>PLAN Conference on Programming Language Design and Implementation (PLDI)
10	<font color="Red">IE</font>EE/ACM International Conference on Automated Software Engineering (ASE)

### 単語境界
##### スペースに囲まれた特定の文字をマッチさせたい「\b」
```bash:「\b」
$ grep --color=always -P '\bo[n|f]\b' computerScienceBooks.txt
1.	ACM/IEEE International Conference on Software Engineering
2.	Journal of Systems and Software
3.	IEEE Transactions on Software Engineering
5.	ACM SIGSOFT Iinternational Symposium on Foundations of Software Engineering
8.	ACM SIGPLAN Conference on Programming Language Design and Implementation (PLDI)
10.	IEEE/ACM International Conference on Automated Software Engineering (ASE)
```
マッチした箇所：
1	ACM/IEEE International Conference <font color="Red">on</font> Software Engineering
2	Journal <font color="Red">of</font> Systems and Software
3	IEEE Transactions <font color="Red">on</font> Software Engineering
5	ACM SIGSOFT Iinternational Symposium on</font> Foundations <font color="Red">of</font> Software Engineering
8	ACM SIGPLAN Conference <font color="Red">on</font> Programming Language Design and Implementation (PLDI)
10	IEEE/ACM International Conference <font color="Red">on</font> Automated Software Engineering (ASE)

### 量指定子(quantifier)
##### 連続した同じ文字にマッチさせたい「{}」
```bash:「{}」
$ egrep --color=always 'E{3}' computerScienceBooks.txt
1.	ACM/IEEE International Conference on Software Engineering
3.	IEEE Transactions on Software Engineering
7.	IEEE Software
10.	IEEE/ACM International Conference on Automated Software Engineering (ASE)
```
マッチした箇所：
1	ACM/I<font color="Red">EEE</font> International Conference on Software Engineering
3	I<font color="Red">EEE</font> Transactions on Software Engineering
7	I<font color="Red">EEE</font> Software
10	I<font color="Red">EEE</font>/ACM International Conference on Automated Software Engineering (ASE)

### アンカー
##### 行の先頭が特定の文字で始まる文字列にマッチさせたい「^」
```bash:「^」
$ grep --color=always '^[4-6]' computerScienceBooks.txt
4.	Information and Software Technology
5.	ACM SIGSOFT Iinternational Symposium on Foundations of Software Engineering
6.	empirical software engineering
```
マッチした箇所：
<font color="Red">4</font>.	Information and Software Technology
<font color="Red">5</font>.	ACM SIGSOFT Iinternational Symposium on Foundations of Software Engineering
<font color="Red">6</font>.	empirical software engineering

##### 行の末尾が特定の文字で終わる文字列にマッチさせたい「$」
```bash:「$」
$ grep --color=always '[e|g]$' computerScienceBooks.txt
1.	ACM/IEEE International Conference on Software Engineering
2.	Journal of Systems and Software
3.	IEEE Transactions on Software Engineering
5.	ACM SIGSOFT Iinternational Symposium on Foundations of Software Engineering
6.	empirical software engineering
7.	IEEE Software
```
マッチした箇所：
1	ACM/IEEE International Conference on Software Engineerin<font color="Red">g</font>
2	Journal of Systems and Softwar<font color="Red">e</font>
3	IEEE Transactions on Software Engineerin<font color="Red">g</font>
5	ACM SIGSOFT Iinternational Symposium on Foundations of Software Engineerin<font color="Red">g</font>
6	empirical software engineerin<font color="Red">g</font>
7	IEEE Softwar<font color="Red">e</font>

### 後方参照
##### HTMLタグにマッチさせたい「\1」
利用するテキスト
```:htmltags.txt
1 This is comment
2 <head>some head</head>
3 <body>some body</body>
4 Hello
5 <p>OK</p>
```

```bash:「\1」
$ egrep --color=always '<([a-zA-Z]*)>.*</\1>' htmltags.txt
2 <head>some head</head>
3 <body>somebody</body>
5 <p>OK</p>
```
補足：HTML開始タグと終了タグが1行に書かれているケースは少なく、実用するためにはより洗練されたものでなければならない。