0
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 3 years have passed since last update.

正規表現 完全一致で置換

Posted at

¥bHOGE¥bのように¥bで文字を囲うと、その文字に完全一致する正規表現になります。¥bは単語境界にマッチするメタ文字です。ここで単語とはa-z, A-Z, 0-9, _から構成されます(英数字とアンダーバー)。

変換前

sky
->sky
blue->sky[0]
blue.sky[0]

blue_sky

コマンド

上記のskyをmoonに変えたい場合、正規表現をs/\bsky\b/moon/gのようにすると、単語のskyのみにマッチします。ちなみにs/sky/moon/gにするとblue_skyまで変換されてしまいます。

perl -wpln -e 's/\bsky\b/moon/g' ./tp.txt > tp_rep.txt 

変換後

moon
->moon
blue->moon[0]
blue.moon[0]

blue_sky

意図通り変換されています。

0
0
1

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?