正規表現オブジェクトの作成

RubyPerl.NET
myregexp = /pattern/
myregexp = Regexp.new(userinput)
$myregexp = qr/pattern/;
$myregexp = qr/$userinput/;
Regexp reg = new Regexp("pattern");
try { Regexp reg = new Regexp(userinput); } catch (ArgumentExeption){}

正規表現オプションの設定

RubyPerl.NET
フリーフォーマット/rまたはRegexp::EXTENED/xRegexOptions.IgnorePatternWhitespace
大文字と小文字を区別しないマッチ/iまたはRegexp::IGNORECASE/iRegexOptions.IgnoreCase
ドットで改行にマッチ/mまたはRegexp::MULTILINE/sRegexOptions.SingleLine
^と$で改行にマッチ常に改行にマッチ/mRegexOptions.MultiLine

日本語正規表現を使うための準備

RubyPerl.NET
ファイルをオープンするときに,"r:utf-8"等,エンコーディングを指定する.
標準出力への書き込みはSTDOUT.set_encoding("CP932")等とする.
use utf8;
use Encode;
ファイルをUTF-8で書く.ファイルをオープンするときに,open my $fh,"<:encoding(cp932)",$file,のようにして,ファイルのエンコーディングを指定する.標準出力への書き込みは binmode STDOUT,":encoding(cp932);"のようにする.
StreamReader("test.txt",Encoding.GetEncoding("shift_jis"));

対象テキスト全体にマッチするかどうかのテスト

RubyPerl.NET
/\Aregex pattern\Z/

マッチしたテキストの取得

RubyPerl.NET
$&$&Regex.Match().Value
  • 後方参照(名前付きキャプチャ)
RubyPerl.NET
$~[name]$+{name}Regex.Match().Groups["name"].Value

全マッチの反復処理

RubyPerl.NET
scan(/regex/) {|match|do something}
gsub(/regex/) {|match|do something}
foreach (/regexp/g) {do something}foreach (Match m in Regex.Matches) {do something}

マッチ中のマッチ

Ruby
scan(/regex/) {|match|match.scan(/Reregex/)
正規表現はネストなどの非線形コンテクストを管理できないので,手続き型コードにまかせるのが賢明.

マッチの置換

RubyPerl.NET
result = subject.gsub(/before/,'after')subject =~ s/before/after/;
別変数に格納
($result = $subject) =~ s/before/after/;
Regex.Replace(subject,"befor","after"

置換テキストの動的生成

RubyPerl.NET
gsub(/\d{4}/) {|match| match = (match.to_i * 2).to_s$s =~ s/\d{4}/$& * 2/eg;(詳細略)MatchEvaluatorを使う

マッチ中のすべての置換

RubyPerl.NET
s = "before <b>first before</b> before <b>before before</b>"
s.gsub!(/<b>.*?<\/b>/m) {|match|match.gsub!(/before/,'after')}
my $s = "before <b>first before</b> before <b>before before</b>";
$s =~ s!<b>.*?</b>!(my $match = $&) =~ s/before/after/g;$match!eg;
(詳細略)MatchEvaluatorを使う

マッチした部分を最利用する置換

RubyPerl.NET
result = subject.gsub(/(\w+) = (\w+)/,'\2=\1')$subject =~ s/(\w+) = (\w+)/$2=$1/gstring resultString = Regexp.Replace(subjectString,@"(\w+)=(\w+)","$2=$1")

マッチ中の隙間に含まれているマッチ

RubyPerl.NET
s = %!"text" <span class="middle">"text"</span> "test"! ->“text” <span class="middle">“text”</span> “test”
result = ""
while s =~ /<[^<>]*>/
match = $&
after = $'
before = $`.gsub(/"([^"]*)"/,'“\1”')
result += before + match
s = after
end
result += after.gsub(/"([^"]*)"/,'“\1”')

文字列の分割

RubyPerl.NET
s = "I like <b>bold</b> and <i> italic </i> fonts"
s.split(/<[^<>]*?>/)
my $s = "I like <b>bold</b> and <i> italic </i> fonts";
say split(/<[^<>]*?>/,$s);
Regex.Split

マッチも残す文字列の分割

RubyPerl.NET
s = "I like <b>bold</b> and <i> italic </i> fonts"
s.split(/(<[^<>]*?>)/)
my $s = "I like <b>bold</b> and <i> italic </i> fonts";
say split(/(<[^<>]*?)>/,$s);
Regex.Split

行単位の検索

RubyPerl.NET
str.each_line {|line| puts line if line =~/reg/}foreach (split(/\r?\n/,$str)) {say if /reg/;}Regex.Split
  • 正規表現には,「この単語を含まない行にマッチせよ」ということを簡単に表現する方法はありません.しかし,あらかじめ行を文字列に分けておけば,単語を含まない行を見つけるのは,各行に対してリテラルテキスト検索をかけて,単語が見つかった行を取り除くだけの簡単な処理になります.

コメントをかく


「http://」を含む投稿は禁止されています。

利用規約をご確認のうえご記入下さい

管理人/副管理人のみ編集できます