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
59k
Regexp_2020
Cybozu
PRO
August 19, 2020
Tweet
Share
More Decks by Cybozu
See All by Cybozu
サイボウズ 開発本部採用ピッチ / Cybozu Engineer Recruit
cybozuinsideout
PRO
10
75k
LLMアプリの品質保証
cybozuinsideout
PRO
1
240
技術広報チームに丸投げしない!「一緒につくる」スポンサー活動
cybozuinsideout
PRO
0
180
kintone開発のプラットフォームエンジニアの紹介
cybozuinsideout
PRO
0
800
テクニカルライター (グループウェア) について
cybozuinsideout
PRO
0
130
つけまが降ってきた日
cybozuinsideout
PRO
1
590
「行ってよかった!」をみんなに広げる
cybozuinsideout
PRO
0
190
サイボウズの QAエンジニアについて / about cybozu QA
cybozuinsideout
PRO
3
4.5k
不具合の先にある面白さ~配属3か月目の新卒QAのいま~
cybozuinsideout
PRO
0
500
Other Decks in Technology
See All in Technology
AIに視覚を与えモバイルアプリケーション開発をより円滑に行う
lycorptech_jp
PRO
1
580
AI活用を"目的"にしたら、データの本質が見えてきた - Snowflake Intelligence実験記 / chasing-ai-finding-data
pei0804
0
830
作るべきものと向き合う - ecspresso 8年間の開発史から学ぶ技術選定 / 技術選定con findy 2026
fujiwara3
6
1.6k
ソフトウェアアーキテクトのための意思決定術: Create Decision Readiness—The Real Skill Behind Architectural Decision
snoozer05
PRO
27
7.7k
全自動で回せ!Claude Codeマーケットプレイス運用術
yukyu30
3
140
2026-02-24 月末 Tech Lunch Online #10 Cloud Runのデプロイの課題から考えるアプリとインフラの境界線
masasuzu
0
100
Webアクセシビリティ技術と実装の実際
tomokusaba
0
150
Secure Boot 2026 - Aggiornamento dei certificati UEFI e piano di adozione in azienda
memiug
0
120
サンタコンペ2025完全攻略 ~お前らの焼きなましは遅すぎる~
terryu16
1
540
AIエージェントで変わる開発プロセス ― レビューボトルネックからの脱却
lycorptech_jp
PRO
2
800
AWS Bedrock Guardrails / 機密情報の入力・出力をブロックする — Blocking Sensitive Information Input/Output
kazuhitonakayama
2
180
【SLO】"多様な期待値" と向き合ってみた
z63d
2
270
Featured
See All Featured
Claude Code のすすめ
schroneko
67
220k
Efficient Content Optimization with Google Search Console & Apps Script
katarinadahlin
PRO
1
360
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
55
3.3k
Speed Design
sergeychernyshev
33
1.6k
The Illustrated Children's Guide to Kubernetes
chrisshort
51
52k
Context Engineering - Making Every Token Count
addyosmani
9
730
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
89
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.7k
Producing Creativity
orderedlist
PRO
348
40k
Building AI with AI
inesmontani
PRO
1
760
Public Speaking Without Barfing On Your Shoes - THAT 2023
reverentgeek
1
330
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
60
42k
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 できたら来年の正規表現講義の講師になるかも