1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Webディレクトリの検索でよく使う正規表現(備忘録)

Last updated at Posted at 2019-01-07

正規表現をしょっちゅう使うわけではないため、割と簡単なものでも都度調べながら作業していました。
今後調べないで済むよう頻繁に使う表現をまとめました。

スラッシュ(/)をエスケープ…バックスラッシュ(\)を使う
// 元の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'\(\)]+\/
1
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?