Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Regexp_2020
Search
Cybozu
PRO
August 19, 2020
Technology
0
58k
Regexp_2020
Cybozu
PRO
August 19, 2020
Tweet
Share
More Decks by Cybozu
See All by Cybozu
生産性向上チームの紹介
cybozuinsideout
PRO
1
870
サイボウズQAの紹介
cybozuinsideout
PRO
1
270
試験仕様書の英語化をやってみたら試験仕様書の本質が見えてきた
cybozuinsideout
PRO
0
270
販売管理オペレーターが開発チームの一員となった話
cybozuinsideout
PRO
0
260
主体的な活動で巨大な影響範囲のテストを乗りこなしていく話
cybozuinsideout
PRO
1
260
Garoon 開発チーム / Garoon development team
cybozuinsideout
PRO
2
2.9k
OSSの脆弱性との向き合い⽅
cybozuinsideout
PRO
2
76
既存プロセスからの脱却と変化に適応するために必要なこと
cybozuinsideout
PRO
2
580
スプリント内で試験を完了させるには?アジャイル・スクラム開発に参加したQAエンジニアの悩みと対策
cybozuinsideout
PRO
1
550
Other Decks in Technology
See All in Technology
ExaDB-D dbaascli で出来ること
oracle4engineer
PRO
0
2.1k
GraphQL 成熟度モデルの紹介と、プロダクトに当てはめた事例 / GraphQL maturity model
mh4gf
7
1.3k
Compose Compiler Metricsを使った実践的なコードレビュー
tomorrowkey
1
220
Além do else! Categorizando Pokemóns com Pattern Matching no JavaScript
wmsbill
0
630
APIファーストなプロダクトマネジメントの実践 〜SaaSus Platformでの例〜 / "Practicing API-First Product Management - An Example with SaaSus Platform
oztick139
0
110
TechFeed Experts Night#27 〜 フロントエンドフレームワーク最前線 (Svelte)
baseballyama
1
530
Building a RAG-poweredAI chat appwith Python and VS Code
pamelafox
0
100
EMとして2023年度に頑張ったこと / What we did well in FY2023 as a EM
pauli
1
170
Google Cloud Next '24でブログを10本書いた方法と勉強会を沸かせた方法
yasumuusan
0
300
Azure Container Apps + Bicep 〜 こんな感じで運用しています
kaz29
2
480
Janus
bkuhlmann
1
490
アクセシビリティを考慮したUI/CSSフレームワーク・ライブラリ選定
yajihum
2
1k
Featured
See All Featured
Six Lessons from altMBA
skipperchong
21
3k
A better future with KSS
kneath
231
16k
Stop Working from a Prison Cell
hatefulcrawdad
266
19k
The Brand Is Dead. Long Live the Brand.
mthomps
49
29k
Navigating Team Friction
lara
178
13k
How to train your dragon (web standard)
notwaldorf
73
5.2k
The Invisible Customer
myddelton
114
12k
A Modern Web Designer's Workflow
chriscoyier
689
190k
Side Projects
sachag
451
41k
Git: the NoSQL Database
bkeepers
PRO
422
63k
The Illustrated Children's Guide to Kubernetes
chrisshort
31
46k
Become a Pro
speakerdeck
PRO
11
4.5k
Transcript
正規表現 サイボウズ株式会社
いきなりですが ▌問題︓あなたが今新しいChrome拡張を開発している。その拡張は、 cybozu.comのサブドメインからのみ使えるようにしたい。どうします︖ n (a) 全てのサブドメインをリストアップして個別にチェックを⼊れる n (b) *.cybozu.com でチェック⼊れる
n (c) /[a-zA-Z0-9_-]+.cybozu.com/ でチェックを⼊れる
(a) 全てのサブドメインをリストアップして個別にチェックを⼊れる ▌全てを表現することが⼤変、不可能な場合もある n hoge.cybozu.com だけ、みたいなケースだと適切 ▌新しいドメインを⼊れたい時更新が必要
(b) *.cybozu.com でチェック⼊れる ▌example.com/www.cybozu.com ▌はい、脆弱性 ▌Glob 表現
正規表現 (Regular Expressions) ▌⽂字列群を⼀つの⽂字列(パターン)でまとめて表現する ▌例)メールアドレス n
[email protected]
n hoge@サイボウズ.co.jp n
[email protected]
n (hoge)
[email protected]
n → RFC5321/RFC5322
正規表現の利⽤例 ▌バリデーション n ⼊⼒された⽂字列が期待通りの形式か n 例)メールアドレス欄に電話番号を⼊⼒していないか ▌抽出 n ⼤量のテキストデータから参照したい部分だけを抽出する n
例)アクセスログからIPアドレスだけを抽出するとか
基本な正規表現: ⽂字列 ▌連続する⽂字列 n 例)cybozu n cybozu.com n hoge.cybozu.com n
hoge.cybozu-dev.com
基本な正規表現: 選択・グループ ▌| 区切りでどれかにマッチさせる ▌() で選択範囲を限定する n 例)com|cn n cybozu.com
n cybozu.cn n (cybozu|kintone).com n cybozu.com n kintone.com n cybozu1com ←あれ︖
基本な正規表現 ▌. 1⽂字(何でも良い) ▌¥ ¥の次の特別⽂字をそのまま使う n 例)¥. 「.」という⽂字 ▌[ab] ⽂字クラス。[]の中の1⽂字をマッチする
▌[^ab] 否定⽂字クラス。[]の中が含まれなかったらマッチする ▌[a-z] aからzまでの⽂字クラス
特別クラス ▌¥d 数字 ▌¥D ⾮数字 ▌¥w ⽂字(数字を含む) ▌¥W ⾮⽂字
基本な正規表現: 繰り返し ▌? 0-1回繰り返しマッチする ▌+ 1回以上繰り返しマッチする ▌* 0回以上繰り返しマッチする ▌{min, max}
min回以上、max回以下 n 例).+¥.cybozu(-dev)?.com n hoge.cybozu-dev.com n hoge.cybozu.com n example.com/www.cybozu.com ←あれ︖
基本な正規表現:先頭と末尾 ▌^ ⾏の先頭にマッチ ▌$ ⾏の末尾にマッチ ▌^hoge.[a-z].com$ n hoge.cybozu.com n hoge.hoge.com
n nothoge.cybozu.com
演習:ここまでの正規表現を使う ▌全てのサイボウズサブドメインを表現しましょう n JP(cybozu.com), US(kintone.com), CN(cybozu.cn)も含む n 開発ドメイン(cybozu-dev.com, kintone-dev.com, …)も含む
n example.com(等) を含まない
後⽅参照 ▌¥1, ¥2, ¥n n番⽬の()内のマッチしたものと同じ⽂字列 ▌<(div|p)>.*<¥/¥1> n <div>hoge</div> n <p>hoge</p>
後⽅参照(2) ▌(?:hoge) マッチはするが、¥1には⼊らない ▌(?<name>hoge)(?P=name) 名前付き後⽅参照グループ
ツール紹介
awk ▌テキスト抽出⽤のプログラミング⾔語・コマンドラインツール ▌Aho, Weinberger, Kernighan ▌正規表現も使える ▌例)ip a s |
awk '/inet/{print $2}' n ip a s の結果に「inet」の含んだ⾏だけに対して2列⽬を抽出する
sed ▌Stream editor ▌ファイルを読み上げて書き換えられる ▌例) sed -i 's/2018/2019/g' kaiun.md ▌今年の資料を作るなら
sed -i 's/2019/2020/g' kaiun.md n kaiun.mdというファイルの中⾝から「2019」という⽂字を全て「2020」に する
grep ▌globally search a regular expression and print ▌全⽂検索してマッチした正規表現をプリントする ▌例)
grep -P "¥d+¥.¥d+¥.¥d+¥.¥d+" /var/log/nginx/error.log n nginxのエラーログからIPアドレスを表⽰する
git-grep ▌https://git-scm.com/docs/git-grep ▌例) git grep -B1 -E "IN¥W+SPF"
普段使っているツールにも ▌CLIでless等を使う場合、結果から正規表現で絞り込む事ができる ▌VSCodeの検索機能には正規表現を使った検索もできる ▌勿論プログラミング⾔語にも n https://github.com/cybozu-go/neco/blob/master/pkg/git- neco/cmd/github.go#L63 n https://github.com/kintone/kintone- cli/blob/master/src/utils/string.ts
お勧め ▌http://shop.oreilly.com/product/9780596003524.do ▌https://blog.cybozu.io/entry/8757 ▌https://regexcrossword.com/ ▌https://regex101.com/
演習 ▌https://regexcrossword.com/ で遊ぶ ▌↓の出⼒からHTTPヘッダーを抽出する n curl -v https://hoge.cybozu-dev.com ▌ログファイルからIPv4アドレスを抽出する n
https://github.com/elastic/examples/raw/master/Common%20Data%20Formats/nginx_logs/ngin x_logs n cat nginx_logs | … | sort --unique ▌地獄級︓RFC5321/RFC5322のメールアドレスをバリデートする正規表現 n できたら来年の正規表現講義の講師になるかも