Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Regexp_2020
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Cybozu
PRO
August 19, 2020
Technology
60k
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Regexp_2020
Cybozu
PRO
August 19, 2020
More Decks by Cybozu
See All by Cybozu
サイボウズ 開発本部採用ピッチ / Cybozu Engineer Recruit
cybozuinsideout
PRO
12
96k
新卒1年目QAが リリース基準の"なぜ"をたどってみた
cybozuinsideout
PRO
1
990
kintone リサーチ副部/UXリサーチャー 業務紹介
cybozuinsideout
PRO
0
220
私たちが『JaSST協賛』から『外部コネクト』チームになった理由
cybozuinsideout
PRO
0
640
LLMでもいつものテスト技術〜意外と半分はこれまでのテストでした〜
cybozuinsideout
PRO
1
1.2k
kintone開発のプラットフォームエンジニアの紹介
cybozuinsideout
PRO
1
1.8k
LLMアプリの品質保証
cybozuinsideout
PRO
1
920
技術広報チームに丸投げしない!「一緒につくる」スポンサー活動
cybozuinsideout
PRO
0
330
テクニカルライター (グループウェア) について
cybozuinsideout
PRO
0
330
Other Decks in Technology
See All in Technology
Oracle Database Security 詳細解説
oracle4engineer
PRO
2
250
生成AI時代のIAMのマニアックな話
takuros
6
1.7k
Maestro MCPとClaude Code Skillsによる自律的なE2Eテスト支援
asmz
0
420
令和の今、あらためて IAM を考える。アクセスキーを「無くせない現実」との向き合い方
kazzpapa3
0
160
サボる子はいねぇがー!AgentCoreハーネスの脆弱性に学ぶ、エージェントの入力バリデーション
iwamot
PRO
4
220
[JAWS Festa 2026]一番いい仕事は、請求書に現れない / jawsfesta2026
sh_fk2
2
160
複雑な業務システムを高速でつくるため、チーム全員をつくれる人へ
ayakurabuchi
0
130
GCASアップデート(202607-202610)
techniczna
1
110
公共システムAdventCalendar2026
toru_kubota
0
200
セキュリティ専任チームなしで攻撃を先回りするSREが実践したSIEM on Amazon OpenSearch Service
cscengineer
PRO
0
230
試験勉強はGrokBotでやれ
maigo999
0
110
新米を「売り越さない」、Aurora DSQLで作る在庫ゼロ死守の限定ドロップEC
yama3133
0
180
Featured
See All Featured
SEO Brein meetup: CTRL+C is not how to scale international SEO
lindahogenes
2
3k
The Cult of Friendly URLs
andyhume
79
7.1k
Marketing to machines
jonoalderson
1
6k
Mind Mapping
helmedeiros
1
430
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
560
More Than Pixels: Becoming A User Experience Designer
marktimemedia
3
620
Rails Girls Zürich Keynote
gr2m
96
14k
Product Roadmaps are Hard
iamctodd
55
13k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
203
77k
Odyssey Design
rkendrick25
PRO
2
920
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
Pawsitive SEO: Lessons from My Dog (and Many Mistakes) on Thriving as a Consultant in the Age of AI
davidcarrasco
0
290
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 できたら来年の正規表現講義の講師になるかも