LoginSignup
0
0

正規表現2023/03/03

Last updated at Posted at 2023-07-04

はじめに

正規表現は裏切らない!
正規表現を一回学習して覚えておけば、マークダウン並みに役立つ

って聞きました
でも、使う機会が少なすぎて忘れちまうんだよ
とりあえずまとめとくか

メタ文字

正規表現で用いられる特殊文字

https://userweb.mnet.ne.jp/nakama/
. ^ $ [ ] * + ? | ( )
メタ文字 意味
. any one string
^@ 行の先頭
@$ 行の末尾
@* 直前の文字が0個以上連続する
@+ 直前の文字が1個以上連続する
@? 直前の文字が0個もしくは1個
@|@ いずれかの文字
[@@@] 指定した文字のどれか
() グループ化

[ ]の中

正規表現 意味 備考
[^@@@] 指定した文字以外のどれか 先頭にある ^
[@-@] 指定した文字範囲のどれか 文字コード上の範囲指定
[\]^.*] ]^.*のどれか 先頭の^,\,]以外の特殊文字が使えない

実用例など

https://qiita.com/Yametaro/items/36493c107053ae996b47
https://www-creators.com/archives/5332
正規表現
意味 備考
^@@@$ 完全一致
^[1-9][0-9]*$ 最初に1〜9が1つ、その後ろに0〜9が0個以上
^.{1,30}$ 1〜30文字 0 < userInput.length && userInput.length <= 30
^(@@@)$ グループ化
^(大正|昭和|平成|令和)$ 大正・昭和・平成・令和だけOK ["大正", "昭和", "平成", "令和"].includes(userInput)
(?!@@@) 否定先読み
^(1st|2nd|3rd|[4-9]th)$ 1桁の英語の序数
^(([0-9]*[02-9])?(1st|2nd|3rd)|([0-9]*([04-9]|1[1-3])th))$ 英語の序数(先頭の0が許容される) 1st, 2nd, 3rd, 4th, 11th, 12th, 13th, 14th, 21st, 22nd, 23rd, 24th
(?=@@@) 肯定先読み
(?=@@@)(?=@@@) AND 含むかつ含む
(?=@@@)(?!@@@) AND 含むかつ含まない
(?=^(([0-9]*[02-9])?(1st|2nd|3rd)|([0-9]*([04-9]|1[1-3])th))$)(?=^[1-9]) 英語の序数 1st, 2nd, 3rd, 4th, 11th, 12th, 13th, 14th, 21st, 22nd, 23rd, 24th

テストサイト

リンク

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0