SlideShare ist ein Scribd-Unternehmen logo
1 von 37
Downloaden Sie, um offline zu lesen
正規表現 超⼊⾨
(Regular Expression)
WSL 勉強会
2016/12/10
友松祐太
2016/12/10 1
はじめに
• 本スライドは本・ネットの資料を参考に作成した資料です。資
料内に誤りがあるかもしれません。。(指摘をお願いします。)
• また、スライド内で出す例はあくまで⼀例に過ぎず、別の書き
⽅であったり、よりスマートな書き⽅などあるかもしれません
のでご了承お願いします。
• このスライドではごく初歩的な正規表現について紹介します。
これを機に正規表現の便利さを少しでも実感してもらって、
⾊々正規表現を触ってもらえるとうれしいです
2016/12/10 2
もくじ
• 正規表現とは
• マッチング
• 1⽂字マッチング
• 繰り返しマッチング
• ⽂末・⽂頭マッチング
• その他の正規表現
• 正規表現による置換
2016/12/10 3
正規表現とは
• ⽂字列のパターンマッチングをするときの表現⽅法
• 正規表現を⽤いることによってあらゆる⽂字列のパターンマッ
チングを⾏うことが出来る。
/^[a-zA-Z0-9.!#$%&ʼ*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:.[a-zA-Z0-9-]+)*$/
2016/12/10 4
もはや暗号。。
正規表現とは
• ⽂字列のパターンマッチングをするときの表現⽅法
• 正規表現を⽤いることによってあらゆる⽂字列のパターンマッ
チングを⾏うことが出来る。
/^[a-zA-Z0-9.!#$%&ʼ*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:.[a-zA-Z0-9-]+)*$/
2016/12/10 5
ある程度この正規表現が読めるようになります!
正規表現とは
• では、どんなときに使う?
• パターンにマッチしたファイル名のファイルを探す(hoge| grep huga)
• フォームに⼊⼒された⽂字列が指定通りになっているかチェック
• ⽂字列置換
• …
ここで紹介しきれないくらいさまざまな利⽤シーンがあります
2016/12/10 6
正規表現とは
• 正規表現は⾔語などの環境によって若⼲⽅⾔が存在します。(ほ
とんどの⽂法は同じ)
• 今回はAtomで使える⽂法でご紹介します
• AtomをインストールしてCmd+Fで
検索画⾯を出して→のような設定にし
てください
2016/12/10 7
正規表現
の利⽤
⼤⽂字と⼩
⽂字の区別
をする
1⽂字マッチング
2016/12/10 8
1⽂字マッチング -特定の1⽂字-
• aとマッチングさせたい
• a
• 単純にaとすれば”a”とマッチング出来る。
• 1とマッチングさせたい
• 1
特定の1⽂字とマッチさせたいときはその⽂字を直接指定すれば
よい。
2016/12/10 9
1⽂字マッチング -数字1⽂字-
• 半⾓数字0,1,2,3,4,5,6,7,8,9のどれか1⽂字とマッチさせたい
• d
• あるいは[0-9]…後程解説
• 数字以外の1⽂字とマッチさせたい
• D
• あるいは[^0-9]…後程解説
2016/12/10 10
1⽂字マッチング -英数字-
• 英数字{a, b, …, z, A, B, …, Z, 0, 1, …, 9,_}1⽂字とのマッチング
• w
• あるいは[a-zA-Z0-9_]
• 英数字以外の1⽂字とのマッチング
• W
• あるいは[^a-zA-Z0-9_]
2016/12/10 11
1⽂字マッチング -空⽩⽂字-
• 空⽩⽂字(半⾓・全⾓スペース、改⾏、タブ等)とマッチング
• s
• あるいは[fnrtv]
• 空⽩⽂字以外とマッチング
• S
• あるいは[^fnrtv]
2016/12/10 12
特殊⽂字
• 改⾏
• n
• タブ区切り
• t
• 正規表現のメタキャラクター
• 先頭にをつける(エスケープする)
• メタキャラクター⼀覧
• . * + ? ( ) [ ] ^ $ 
2016/12/10 13
1⽂字マッチング
-候補マッチング-
• 候補の中から1⽂字にマッチング
• []の中にマッチさせたい1⽂字の候補を羅列する
例) [abc]
aかbかcにマッチングする
例) [012]
0か1か2にマッチングする
• 候補以外の1⽂字にマッチング
• [^]の中にマッチさせたい1⽂字の候補を羅列
例) [^abc]
a,b,c以外の1⽂字にマッチ
2016/12/10 14
1⽂字マッチング
-候補マッチング(範囲指定)-
• 候補の中の1⽂字にマッチング【応⽤編】
• [a-z] のように指定すると a~z のどれか1⽂字にマッチさせられる。
例)[^a-zA-Z0-9_]
英数字アンスコ以外の1⽂字にマッチング(=W)
2016/12/10 15
1⽂字マッチング -任意の1⽂字-
• なんでもいいので1⽂字とマッチさせたい
• .(ピリオド)
例)A.Cでマッチング
ABC, AAC, AaC, A1C, …などにマッチする
2016/12/10 16
1⽂字マッチング
構⽂ マッチする⽂字
a “a”とマッチ
d 数字1⽂字とマッチ
D 数字以外の1⽂字とマッチ
s 空⽩⽂字1⽂字とマッチ
S 空⽩⽂字以外の1⽂字とマッチ
w 英数字アンスコ1⽂字とマッチ[a-zA-Z0-9_]
W 英数字アンスコ以外の1⽂字とマッチ
. 任意の1⽂字とマッチ
[…] []の中に羅列した中の1⽂字にマッチ
[^… ] [^]の中に羅列した⽂字以外にマッチ
[a-z] aからzの中のどれか1⽂字にマッチ
2016/12/10 17
繰り返しマッチング
2016/12/10 18
繰り返しマッチング
• 直前のパターンの1回以上の繰り返し
• “+”(プラス記号)
例1) GRe+N
GReN, GReeN, GReeeN, GReeeeN, GReeeeeN,…
にマッチする
例2)d+
0,1,2,11,12,00000,999231231,23454,…
数字の連続にマッチする。
2016/12/10 19
繰り返しマッチング
• 直前のパターンの0回以上の繰り返し
• *(アスタリスク)
例)GRe*N
GRN, GReN, GReeN, GReeeN, GReeeeN, GReeeeeN,…にマッチ
↑*だとこのパターンが存在
2016/12/10 20
繰り返しマッチング
• 直前のパターンの0回か1回の繰り返し
• ?
例)apples?
appleとapplesにマッチする
look(ed)?
lookとlookedにマッチする
2016/12/10 21
繰り返しマッチング
• 直前のパターンの任意の回数の繰り返し
• {n} …n回の繰り返し
• {n,m} …n回以上,m回以下の繰り返し
• {0,1}は?と同値
• {n,} …n回以上の繰り返し
• {0,}は*と同値、{1,}は+と同値
例)
GRe{4}N … GReeeeNにマッチ
GRe{4,}N … GReeeeN, GReeeeeN, GReeeeeeN,…にマッチ
2016/12/10 22
繰り返しマッチング
正規表現
* 直前のパターンの0回以上の繰り返し
+ 直前のパターンの1回以上の繰り返し
? 直前のパターンの0回または1回
{n} 直前のパターンのn回の繰り返し
{n,m} 直前のパターンのn回以上m回以下の繰り返し
{n,} 直前のパターンのn回以上の繰り返し
2016/12/10 23
⽂末・⽂頭マッチング
2016/12/10 24
⽂頭記号/⽂末記号
• ⽂頭は^で表す。
• 正規表現の先頭に^を指定する。
• ⽂末表現は$で表す。
• 正規表現の末尾に$を指定する。
例)Thisで始まって.(ピリオド)で終わる⾏とのマッチ
^This.*.$
2016/12/10 25
その他の正規表現
2016/12/10 26
ORマッチ
• 2つの⽂字列のどちらかにマッチさせたい。
• |(パイプ)で結ぶ
例)Windows|WINDOWS
WindowsあるいはWINDOWSにマッチ
例)Windows|WINDOWS|windows
Windows,WINDOWS,windowsにマッチ
2016/12/10 27
ブロック
• ()でくくるとブロック化出来ます。
例 (ab)+
ab,abab,ababab,…にマッチする。
例 W(indows|INDOWS)
Windows,WINDOWSにマッチ
例 GRee(ee)?N
GReeN,GReeeeNにマッチ
2016/12/10 28
正規表現による置換
2016/12/10 29
正規表現による置換
• 正規表現による置換を使うことで「ちょっとしたデータ整形を
したい」や「この部分取ってきたい」みたいな作業をやるには
かなり有効である。
• いろいろな例で説明します!
2016/12/10 30
正規表現による置換
• ()で括ると()内のマッチした部分を記憶する(取り出す)ことがで
きる。
例) www.example.com/layer_1/layer_2/ex.html
ここからドメインだけを取得したい
^([^/]+)/.*$ ---replace--> $1
結果:www.example.com
2016/12/10 31
カッコで括った部分
にマッチした部分を
$で取得することが
できる
正規表現による置換
• 空⾏を消したい
• ^n ---replace--> (空⽂字列)
• ⽂末のカンマを消したい
• ,$ ---replace--> (空⽂字列)
• TSV,CSVの各要素を””で囲いたい
• ([^t]+) ---replace--> ”$1”
• ([^,]+) ---replace--> ”$1”
2016/12/10 32
正規表現による置換
• 情報科学科の学籍番号をmeijiメールのアドレスに変換
d{3}Rd(d{5}) ---replace--> ee$1@meiji.ac.jp
例えば…
157R127112 --replace--> ee27112@meiji.ac.jp
• 学籍番号を何年度卒業の何番に変換
d{3}Rd(d{2})(d{3}) ---replace--> 平成$1年度卒業$2番
例えば…
157R127112 ---replace--> 平成27年度⼊学112番
2016/12/10 33
正規表現による置換
• htmlからタグだけを取り出す
• (<[^>]*>) ---replace--> n$1n
• ^(n|[^<])* ---replace--> 空⽂字列
2016/12/10 34
CSV・TSVのカラム取り出し
• CSVのk列⽬のカラムを取り出したい。
^([^,n]+,){k-1}([^,n]+)[^n]*$ ---replace--> $2
• TSVのk列⽬のカラムを取り出したい。
^([^tn]+t){k-1}([^tn]+)[^n]*$ ---replace--> $2
2016/12/10 35
Thank you for your attention!
2016/12/10 36
参考
• (メールアドレスを表す現実的な正規表現)
[http://qiita.com/sakuro/items/1eaa307609ceaaf51123]
• (サルにもわかる正規表現⼊⾨)
[http://www.mnet.ne.jp/~nakama/]
• (正規表現サンプル集 - メガソフト トップ)
[http://www.megasoft.co.jp/mifes/seiki/]
• (正規表現 Wikipedia)
[https://ja.wikipedia.org/wiki/%E6%AD%A3%E8%A6%8F%E8%
A1%A8%E7%8F%BE]
2016/12/10 37

Weitere ähnliche Inhalte

Empfohlen

AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 

Empfohlen (20)

AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 

Regular Expression