2
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?

業務で○○年△月(西暦下二桁+1~12月)形式のデータをYYYYMM形式への変換を行ったので、正規表現の使い方の記録として残します。

手順

①00年~24年のデータを2000年代として置換

< \b(0[0-9]|1[0-9]|2[0-4])年(\d{1,2})\b
> 20$1$2

②残りのデータを1900年代として置換

< \b(\d{2})年(\d{1,2})\b
> 19$1$2

③1~9月を01月~09月に変換

< \b(\d{4})年(\d{1})\b
> $1年0$2

正規表現の復習

[] {}については省略。

文字 説明
\b 単語の先頭・末尾を表す
\d{n} n桁の数字を表す
$n n番目のキャプチャグループを表す

※キャプチャグループ……正規表現の中で()でまとめられた要素

参考:https://qiita.com/List202/items/05a27c46d9b11d04fb39

2
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
2
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?