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_2021
Search
Cybozu
PRO
June 02, 2021
Technology
3
12k
正規表現 / RegExp_2021
Cybozu
PRO
June 02, 2021
Tweet
Share
More Decks by Cybozu
See All by Cybozu
PSIRTでAIテストを実施するまでの道のり
cybozuinsideout
PRO
0
79
無理なく続けるサイボウズの社内勉強会
cybozuinsideout
PRO
1
1.1k
分散システムにおける 無兆候データ破損の影響について
cybozuinsideout
PRO
1
48
タンパク質構造のシミュレーションソフトウェア試行錯誤
cybozuinsideout
PRO
1
37
読みやすいアセンブリ言語
cybozuinsideout
PRO
1
30
Wasmで拡張できる軽量マークアップ⾔語Brack(後編)
cybozuinsideout
PRO
1
26
Wasmで拡張できる軽量マークアップ⾔語Brack(前編)
cybozuinsideout
PRO
1
26
kintone開発組織のAWSエンジニアの紹介
cybozuinsideout
PRO
0
210
kintone開発組織のサービスプラットフォームチームの紹介
cybozuinsideout
PRO
0
110
Other Decks in Technology
See All in Technology
面接を通過するためにやってて良かったこと3選
sansantech
PRO
0
130
ローカル環境でAIを動かそう!
falken
PRO
1
170
セキュリティSaaS企業が実践するCursor運用ルールと知見 / How a Security SaaS Company Runs Cursor: Rules & Insights
tetsuzawa
0
450
S3 Tables を図解でやさしくおさらい~基本から QuickSight 連携まで/s3-tables-illustrated-basics-quicksight
emiki
2
340
Devin&Cursor、それぞれの「本質」から導く最適ユースケース戦略
empitsu
8
2.5k
うちの会社の評判は?SNSの投稿分析にAIを使ってみた
doumae
0
250
ソフトウェアは捨てやすく作ろう/Let's make software easy to discard
sanogemaru
10
5.8k
技術書典18結果報告
mutsumix
2
180
toittaにOpenTelemetryを導入した話 / Mackerel APM リリースパーティ
cohalz
1
490
Redmineの意外と知らない便利機能 (Redmine 6.0対応版)
vividtone
0
1.2k
CSSDay, Amsterdam
brucel
0
130
大事なのは、AIの精度だけじゃない!〜1円のズレも許されない経理領域とAI〜
jun_nemoto
11
5.2k
Featured
See All Featured
Being A Developer After 40
akosma
91
590k
Scaling GitHub
holman
459
140k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
331
21k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
52
2.8k
How to train your dragon (web standard)
notwaldorf
92
6k
The Power of CSS Pseudo Elements
geoffreycrofte
76
5.8k
[RailsConf 2023] Rails as a piece of cake
palkan
55
5.6k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
357
30k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.3k
Designing for Performance
lara
608
69k
Building an army of robots
kneath
306
45k
The Cost Of JavaScript in 2023
addyosmani
49
8.1k
Transcript
正規表現 サイボウズ株式会社
名前とは︖ ▌全てをリストアップしようとしたら 切りがない ▌「何でも良い」というわけにもい かない ▌(なるべく)全てを表現するパ ターンを使う
正規表現の利⽤例 ▌バリデーション 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 例)(cybozu|kintone)¥.com ▌[ab] ⽂字クラス。[]の中の1⽂字をマッチする ▌[^ab]
否定⽂字クラス。[]の中が含まれなかったらマッチする ▌[a-z] aからzまでの⽂字クラス
特別クラス ▌¥d 数字 →[0-9] ▌¥D ⾮数字 →[^0-9] ▌¥w ⽂字(数字を含む) →[a-zA-Z0-9_]
▌¥W ⾮⽂字 →[^a-zA-Z0-9_]
基本な正規表現: 繰り返し ▌? 0-1回繰り返しマッチする ▌+ 1回以上繰り返しマッチする ▌* 0回以上繰り返しマッチする ▌{min, max}
min回以上、max回以下 n 例).+¥.cybozu(-dev)?.com n ocean.cybozu-dev.com n hoge.cybozu.com n example.com/www.cybozu.com ←あれ︖
名前とは︖ ▌[A-Z][a-z]* ▌本当︖ ▌「Taro Jr.」 は︖ ▌正規表現って難しい
基本な正規表現:先頭と末尾 ▌^ ⾏の先頭にマッチ ▌$ ⾏の末尾にマッチ ▌^hoge.[a-z].com$ n hoge.cybozu.com n hoge.hoge.com
n nothoge.cybozu.com
後⽅参照 ▌¥1, ¥2, ¥n n番⽬の()内のマッチしたものと同じ⽂字列 ▌<(div|p)>.*<¥/¥1> n <div>hoge</div> n <p>hoge</p>
後⽅参照(2) ▌(?:hoge) マッチはするが、¥1には⼊らない ▌(?<name>hoge)(?P=name) 名前付き後⽅参照グループ
メールアドレスとは︖ ▌ (?:[a-z0-9!#$%&'*+¥/=?^_`{|}~-]+(?:¥.[a- z0-9!#$%&'*+¥/=?^_`{|}~-]+)*|"(?:[¥x01- ¥x08¥x0b¥x0c¥x0e-¥x1f¥x21¥x23- ¥x5b¥x5d-¥x7f]|¥¥[¥x01- ¥x09¥x0b¥x0c¥x0e-¥x7f])*")@(?:(?:[a- z0-9](?:[a-z0-9-]*[a-z0-9])?¥.)+[a-z0- 9](?:[a-z0-9-]*[a-z0-9])?|¥[(?:(?:(2(5[0- 5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-
9]))¥.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0- 9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0- 9]:(?:[¥x01-¥x08¥x0b¥x0c¥x0e-¥x1f¥x21- ¥x5a¥x53-¥x7f]|¥¥[¥x01- ¥x09¥x0b¥x0c¥x0e-¥x7f])+)¥]) ▌ 正規表現って難しい
ツール紹介 業務で恐らく使う事になる場⾯
awk ▌テキスト抽出⽤のプログラミング⾔語・コマンドラインツール ▌Aho, Weinberger, Kernighan ▌正規表現も使える ▌例)ip a s |
awk '/inet/{print $2}' n ip a s の結果に「inet」の含んだ⾏だけに対して2列⽬を抽出する
sed ▌Stream editor ▌ファイルを読み上げて書き換えられる ▌例) sed -i 's/2018/2021/g' kaiun.md ▌今年の資料を作るなら
sed -i 's/2019/2021/g' kaiun.md n kaiun.mdというファイルの中⾝から「2019」という⽂字を全て「2021」に する
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する ▌例) 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