$30 off During Our Annual Pro Sale. View Details »
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Regexp_2020
Search
Cybozu
PRO
August 19, 2020
Technology
0
59k
Regexp_2020
Cybozu
PRO
August 19, 2020
Tweet
Share
More Decks by Cybozu
See All by Cybozu
つけまが降ってきた日
cybozuinsideout
PRO
1
390
「行ってよかった!」をみんなに広げる
cybozuinsideout
PRO
0
130
不具合の先にある面白さ~配属3か月目の新卒QAのいま~
cybozuinsideout
PRO
0
360
kintone開発チームの紹介
cybozuinsideout
PRO
1
82k
kintone開発のプラットフォームエンジニアの紹介
cybozuinsideout
PRO
0
340
AIツール開発ワークショップ(Dify)【サイボウズ新人研修2025】
cybozuinsideout
PRO
22
25k
モバイル【サイボウズ新人研修2025】
cybozuinsideout
PRO
3
4.4k
Git/GitHub を使う上で知っておくと嬉しいかも Tips【サイボウズ新人研修2025】
cybozuinsideout
PRO
16
11k
GitHub Copilot活用【サイボウズ新人研修2025】
cybozuinsideout
PRO
15
16k
Other Decks in Technology
See All in Technology
A Compass of Thought: Guiding the Future of Test Automation ( #jassttokai25 , #jassttokai )
teyamagu
PRO
1
250
Lessons from Migrating to OpenSearch: Shard Design, Log Ingestion, and UI Decisions
sansantech
PRO
1
120
モダンデータスタック (MDS) の話とデータ分析が起こすビジネス変革
sutotakeshi
0
450
EM歴1年10ヶ月のぼくがぶち当たった苦悩とこれからへ向けて
maaaato
0
270
Playwright x GitHub Actionsで実現する「レビューしやすい」E2Eテストレポート
kinosuke01
0
550
30分であなたをOmniのファンにしてみせます~分析画面のクリック操作をそのままコード化できるAI-ReadyなBIツール~
sagara
0
120
AWSを使う上で最低限知っておきたいセキュリティ研修を社内で実施した話 ~みんなでやるセキュリティ~
maimyyym
2
270
AWS CLIの新しい認証情報設定方法aws loginコマンドの実態
wkm2
6
700
Challenging Hardware Contests with Zephyr and Lessons Learned
iotengineer22
0
180
AI 駆動開発勉強会 フロントエンド支部 #1 w/あずもば
1ftseabass
PRO
0
320
re:Invent 2025 ~何をする者であり、どこへいくのか~
tetutetu214
0
210
GitHub Copilotを使いこなす 実例に学ぶAIコーディング活用術
74th
3
2.5k
Featured
See All Featured
How to Ace a Technical Interview
jacobian
280
24k
Making Projects Easy
brettharned
120
6.5k
The World Runs on Bad Software
bkeepers
PRO
72
12k
Balancing Empowerment & Direction
lara
5
790
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
9.8k
Reflections from 52 weeks, 52 projects
jeffersonlam
355
21k
Rails Girls Zürich Keynote
gr2m
95
14k
Art, The Web, and Tiny UX
lynnandtonic
303
21k
A designer walks into a library…
pauljervisheath
210
24k
Git: the NoSQL Database
bkeepers
PRO
432
66k
Imperfection Machines: The Place of Print at Facebook
scottboms
269
13k
Agile that works and the tools we love
rasmusluckow
331
21k
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 できたら来年の正規表現講義の講師になるかも