2
1

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.

現場で使える正規表現

Last updated at Posted at 2019-04-16

正規表現

・社内勉強会用のメモ
・サクラエディタを前提に説明する
・知ってると作業効率がよくなる

なにができるの

・テキストの整形
(CSV→TSV、Excelから取ってきた改行付きテーブルの論理名をCSVにしたり…)
・Grep(影響範囲調査等)

 覚えておきたい表現

表現 意味
. 任意の一文字
* 0個以上の繰り返し
+ 1個以上の繰り返し
\t タブ
\r\n 改行(CRLF)
^ 行頭
$ 行末
[0-9] 数字
[a-z] アルファベット小文字
[A-Z] アルファベット大文字
([A-Z]) ()内でマッチした内容を$1で取り出す

|⇒表現をorで繋げる

ほかにも色々、詳しくは公式見てね
置換表現とか細かいのはライブラリのページを

・CSV→TSV
,→\tに変換

aaa,bb,ccc,ddd,eeee
↓
aaa bbb ccc ddd eeee

・改行をカンマに変換
\r\n→,

aaa
bbb
ccc
↓↓↓
aaa,bbb,ccc

・先頭が数字で始まる行を探す
^[0-9]

0asd   #=> match
daee
4ghsf  #=> match

・最後が数字で終わる行を探す
[0-9]$

asdda3  #=> match
da4dff
gea123  #=> match

・数字が1個以上続いてるところを探す
[0-9]+

a23dfa #=> match
add221 #=> match
2daf2 #=> match
adffe

・AまたはBでGREPする(DecimalClass|NumericClass)

・キャメルケースに変換

_([a-z])→\U$1\E
※Perl記法が有効なエンジンの場合

request_no  #=> request_No
regi_order_no #=> regi_Order_No

おまけ

サクラエディタの使い方

検索(Ctrl+F)

正規表現チェック入れるよ.PNG
※正規表現を使うにチェックを入れる必要あり

置換(Ctrl+R)

置換.PNG

Grep(Ctrl+G)

GREP.PNG

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?