正規表現をしょっちゅう使うわけではないため、割と簡単なものでも都度調べながら作業していました。
今後調べないで済むよう頻繁に使う表現をまとめました。
スラッシュ(/)をエスケープ…バックスラッシュ(\)を使う
// 元のWebディレクトリ:/toy/puzzle/castle/list
\/toy\/puzzle\/castle\/list
バックスラッシュ(\)をエスケープ…同上
// 元のWebディレクトリ:\toy\puzzle\castle\list
\\toy\\puzzle\\castle\\list
(以降、ディレクトリの区切り文字はスラッシュで説明します)
前方一致
^\/toy\/puzzle
後方一致
// /listで終わる
\/list$
// /item+数字3桁(例「/item123」)で終わる
\/item[0-9]{3}$
完全一致
^\/toy\/puzzle\/castle\/list$
部分一致
// 文字列「/puzzle/」を含む
\/puzzle\/
任意のアイテム番号を検索
// item+数字3桁を含む
item[0-9]{3}
// /item+数字3桁 または itemを含む
item([0-9]{3})?
// 数字が0文字以上続く
item[0-9]*
// 数字が1文字以上続く
item[0-9]+
ディレクトリの末尾を検索
// 末尾がitemまたはlistで終わる
\/toy\/puzzle\/castle\/(item|list)$
任意のディレクトリを検索
// puzzleの特定のカテゴリを検索
\/toy\/puzzle\/(castle|forest|christmas)\/
// 全種類のpuzzleを検索
\/toy\/puzzle\/[a-z]+\/
// /toy/puzzle/配下の全ディレクトリを検索
\/toy\/puzzle\/[a-zA-Z0-9\/\-_\.!\s'\(\)]+\/