正規表現:例1

文字列 "abcadcaecafc" 中の "a"(1文字)"c" を全て "axc" に置換する。

emacsperlWord

emacsの場合

青:フレームの表示  赤:コマンド入力  緑:emacsメッセージ 

abcadcaecafc
M-x replace-regexp <RET> a.c <RET> axc <RET>axcaxcaxcaxc 
Replaced 4 occurrences
  

参考サイト:GNU Emacsマニュアル: 探索と置換

perlの場合

ファイル「ex001.pl」の内容。

#!/usr/local/bin/perl ←perlプログラムの場所
$string = "abcadcaecafc";
($replaced = $string) =~ s/a.c/axc/g;
print $string,"¥n",$replaced,"¥n";
  

"=~"は「パターン結合演算子」と呼ばれ、「左辺の値から右辺の値を検索する」を意味する。

コマンド実行の様子。

shell> chmod +x ex001.pl<RET>←ファイルに実行権限を与える
shell> ./ex001.pl<RET>
abcadcaecafc
axcaxcaxcaxc
shell> 
  

Wordの場合

検索する文字列
a?c
置換後の文字列
axc