Regex.Replaceとは? わかりやすく解説

Regex.Replace メソッド (String, String, Int32, Int32)

入力文字列の指定した文字位置から検索を開始し、Regex コンストラクタで指定した正規表現によって定義されている入力文字列内のパターンに一致する対象を指定数だけ指定の置換文字列で置き換えます。

名前空間: System.Text.RegularExpressions
アセンブリ: System (system.dll 内)
構文構文

解説解説

count が負の場合、置換は文字列の末尾まで継続されます。その他の 2 つの形の置換では、count (-1) および startat (左から右方向の場合は 0、右から左方向の場合は input.Length) に対して既定値が指定されています。

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

Regex.Replace メソッド (String, String, String, RegexOptions)

入力文字列内の最初の文字から検索を開始し、指定した正規表現によって定義されているパターンに一致するすべての対象を指定の置換文字列で置き換えます。一致の動作を変更するオプションを指定できます。

名前空間: System.Text.RegularExpressions
アセンブリ: System (system.dll 内)
構文構文

解説解説
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

Regex.Replace メソッド (String, String)

入力文字列の最初の文字から検索を開始し、指定した正規表現パターンに一致する対象をすべて置換文字列で置き換えます。

名前空間: System.Text.RegularExpressions
アセンブリ: System (system.dll 内)
構文構文

使用例使用例

Regex.Replace メソッドのコード例を次に示します。

' This code example demonstrates the System.Text.Regular-
' Expressions.Regex.Replace(String, String) method.

Imports System
Imports System.Text.RegularExpressions

Class Sample
    Public Shared Sub Main()
 
        ' Create a regular expression that matches a series of one 
        ' or more white spaces.
        Dim pattern As String
 = "\s+"
        Dim rgx As New Regex(pattern)
        
        ' Declare a string consisting of text and white spaces.
        Dim inputStr As String
 = "a   b   c   d"
        
        ' Replace runs of white space in the input string with a
        ' comma and a blank.
        Dim outputStr As String
 = rgx.Replace(inputStr, ", ")
        
        ' Display the resulting string.
        Console.WriteLine("Pattern:       ""{0}""",
 pattern)
        Console.WriteLine("Input string:  ""{0}""",
 inputStr)
        Console.WriteLine("Output string: ""{0}""",
 outputStr)
    End Sub 'Main
End Class 'Sample

'
'This code example produces the following results:
'
'Pattern:       "\s+"
'Input string:  "a   b   c   d"
'Output string: "a, b, c, d"
'
// This code example demonstrates the System.Text.Regular-
// Expressions.Regex.Replace(String, String) method.

using System;
using System.Text.RegularExpressions;

class Sample 
{
    public static void Main()
 
    {
// Create a regular expression that matches a series of one 
// or more white spaces.
    string pattern = @"\s+";
    Regex rgx = new Regex(pattern);

// Declare a string consisting of text and white spaces.
    string inputStr = "a   b   c   d";

// Replace runs of white space in the input string with a
// comma and a blank.
    string outputStr = rgx.Replace(inputStr, ", ");

// Display the resulting string.
    Console.WriteLine("Pattern:       \"{0}\"", pattern);
    Console.WriteLine("Input string:  \"{0}\"",
 inputStr);
    Console.WriteLine("Output string: \"{0}\"",
 outputStr);
    }
}
/*
This code example produces the following results:

Pattern:       "\s+"
Input string:  "a   b   c   d"
Output string: "a, b, c, d"

*/
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

Regex.Replace メソッド (String, MatchEvaluator)

Regex コンストラクタで指定された正規表現によって定義されている文字列パターンに一致するすべての対象を置き換えます。MatchEvaluator デリゲートは、一致する対象が見つかるたびに置換を評価するために呼び出されます。

名前空間: System.Text.RegularExpressions
アセンブリ: System (system.dll 内)
構文構文

Public Function Replace ( _
    input As String, _
    evaluator As MatchEvaluator _
) As String
Dim instance As Regex
Dim input As String
Dim evaluator As MatchEvaluator
Dim returnValue As String

returnValue = instance.Replace(input, evaluator)
public string Replace (
    string input,
    MatchEvaluator evaluator
)
public:
String^ Replace (
    String^ input, 
    MatchEvaluator^ evaluator
)
public String Replace (
    String input, 
    MatchEvaluator evaluator
)
public function Replace (
    input : String, 
    evaluator : MatchEvaluator
) : String

パラメータ

input

変更対象の文字列。

evaluator

置換を 1 つずつ評価する MatchEvaluator。

戻り値
変更後の文字列。

解説解説

MatchEvaluator 型は、入力として単一の Match を取得し、文字列を返すデリゲートです。次のように宣言します。

public delegate String RegexMatchEvaluator(Match match);

このデリゲートは、一致する対象が置換中に見つかるたびに呼び出されます。

使用例使用例

最初に検索対象の文字列を表示し、その文字列からパターンに一致する各単語を検索した後、一致した各単語の先頭文字を大文字に変換して表示するコード例を次に示します。

Imports System.Text.RegularExpressions

Class RegExSample
   Shared Function CapText(m As
 Match) As String
      ' Get the matched string.
      Dim x As String =
 m.ToString()
      ' If the first char is lower case...
      If Char.IsLower(x.Chars(0)) Then
         ' Capitalize it.
         Return Char.ToUpper(x.Chars(0)) &
 x.Substring(1, x.Length - 1)
      End If
      Return x
   End Function    
    
   Public Shared Sub Main()
      Dim text As String
 = "four score and seven years ago"
      System.Console.WriteLine("text=[" & text
 & "]")
      Dim result As String
 = Regex.Replace(text, "\w+", _
         AddressOf RegExSample.CapText)
      System.Console.WriteLine("result=[" & result
 & "]")
   End Sub
End Class
using System.Text.RegularExpressions;

class RegExSample 
{
   static string CapText(Match m) 
   {
      // Get the matched string.
      string x = m.ToString();
      // If the first char is lower case...
      if (char.IsLower(x[0])) 
      {
         // Capitalize it.
         return char.ToUpper(x[0]) + x.Substring(1,
 x.Length-1);
      }
      return x;
   }
    
   static void Main() 
   {
      string text = "four score and seven years ago";
      System.Console.WriteLine("text=[" + text + "]");
      string result = Regex.Replace(text, @"\w+",
         new MatchEvaluator(RegExSample.CapText));
      System.Console.WriteLine("result=[" + result + "]");
   }
}
#using <System.dll>

using namespace System;
using namespace System::Text::RegularExpressions;

ref class RegExSample
{
public:
   static String^ CapText( Match^ m )
   {
      // Get the matched String.
      String^ x = m->ToString();
      
      // If the first char is lower case...
      if ( Char::IsLower( x[ 0 ] ) )
      {
         // Capitalize it.
         return Char::ToUpper( x[ 0 ] ) + x->Substring( 1,
 x->Length - 1 );
      }

      return x;
   }
};

int main()
{
   String^ text = "four score and seven years ago";
   Console::WriteLine( "text=[{0}]", text );
   String^ result = Regex::Replace( text, "\\w+", gcnew MatchEvaluator(
 &RegExSample::CapText ) );
   System::Console::WriteLine( "result=[{0}]", result );
}
import System.Text.RegularExpressions.*;

class RegExSample
{
    static String CapText(Match m)
    {
        // Get the matched string.
        String x = m.ToString();

        // If the first char is lower case...
        if (System.Char.IsLower(x.charAt(0))) {
            // Capitalize it.
            return System.Char.ToUpper(x.charAt(0)) 
                + x.Substring(1, x.get_Length() - 1);
        }
        return x;
    } //CapText

    public static void main(String[]
 args)
    {
        String text = "four score and seven years ago";
        System.Console.WriteLine("text=[" + text + "]");
        String result = Regex.Replace(text, "\\w+", 
            new MatchEvaluator(RegExSample.CapText));
        System.Console.WriteLine("result=[" + result + "]");
    } //main
} //RegExSample
import System.Text.RegularExpressions;

class RegExSample 
{ 
   static function CapText(m : Match) : String
  
   {
      // get the matched string.
      var x : String = m.ToString();
      // If the first char is lower case...
      if (System.Char.IsLower(x[0])) 
      {
         // Capitalize it.
         return System.Char.ToUpper(x[0]) + x.Substring(1, x.Length-1);
      }
      return x;
   }
    
   static function Main() : void
 
   {
      var text : String = "four score and seven years ago";
      System.Console.WriteLine("text=[" + text + "]");
      var result : String = Regex.Replace(text, "\\w+",
 RegExSample.CapText);
      System.Console.WriteLine("result=[" + result + "]");
   }
}
RegExSample.Main();
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

Regex.Replace メソッド (String, MatchEvaluator, Int32)

入力文字列の最初の文字から検索を開始し、Regex コンストラクタで指定した正規表現によって定義されているパターンと一致する対象を指定数だけ置換文字列で置き換えます。MatchEvaluator デリゲートは、一致する対象が見つかるたびに置換を評価するために呼び出されます。

名前空間: System.Text.RegularExpressions
アセンブリ: System (system.dll 内)
構文構文

Public Function Replace ( _
    input As String, _
    evaluator As MatchEvaluator, _
    count As Integer _
) As String
Dim instance As Regex
Dim input As String
Dim evaluator As MatchEvaluator
Dim count As Integer
Dim returnValue As String

returnValue = instance.Replace(input, evaluator, count)
public string Replace (
    string input,
    MatchEvaluator evaluator,
    int count
)
public:
String^ Replace (
    String^ input, 
    MatchEvaluator^ evaluator, 
    int count
)
public String Replace (
    String input, 
    MatchEvaluator evaluator, 
    int count
)
public function Replace (
    input : String, 
    evaluator : MatchEvaluator, 
    count : int
) : String

パラメータ

input

変更対象の文字列。

evaluator

置換を 1 つずつ評価する MatchEvaluator。

count

置換を行う最大回数。

戻り値
変更後の文字列。

解説解説

MatchEvaluator 型は、入力として単一の Match を取得し、文字列を返すデリゲートです。次のように宣言します。

public delegate String RegexMatchEvaluator(Match match);

このデリゲートは、一致する対象が置換中に見つかるたびに呼び出されます。

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

Regex.Replace メソッド (String, String, String)

入力文字列の最初の文字から検索を開始し、正規表現によって定義されている文字パターンに一致するすべての対象を置換文字列で置き換えます。

名前空間: System.Text.RegularExpressions
アセンブリ: System (system.dll 内)
構文構文

解説解説

置き換えは置換パターン内でだけ実行できます。正規表現の中で同様の機能を利用するには、\1 などの backreference を使用します。

文字のエスケープと置き換えは、置換パターンで認識される唯一の特殊な構成体です。その他のすべての構文構成体は正規表現だけで使用でき、置換パターンでは認識されません。たとえば、置換パターンが a*${test}b の場合は、文字列 "a*" が挿入され、その後ろに "test" キャプチャ グループと一致する部分文字列 (存在する場合) が続き、さらに文字列 "b" が続きます。* 文字は置換パターン内ではメタ文字として認識されません。同様に、$ パターンも正規表現一致パターン内では認識されません。正規表現内では、$ は文字列の末尾を示します。その他の例として、$123 はグループ番号 123 (10 進数) に一致した最後の部分文字列に置き換えられ、${name) は (?<name>) グループと一致する最後の部分文字列に置き換えられます。

静的 Replace メソッドは、正規表現パターンを指定して Regex オブジェクトを構築し、インスタンス メソッド Replace を呼び出す操作と同じです。この静的メソッドは、Regex オブジェクトを明示的に作成しなくても、正規表現を単独で 1 回だけ使用できるようにする目的で用意されています。

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

Regex.Replace メソッド (String, String, Int32)

入力文字列の最初の文字から検索を開始し、Regex コンストラクタで指定した正規表現で定義されているパターンに一致する対象を指定数だけ置換文字列で置き換えます。

名前空間: System.Text.RegularExpressions
アセンブリ: System (system.dll 内)
構文構文

解説解説
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

Regex.Replace メソッド (String, String, MatchEvaluator)

最初の文字から検索を開始し、正規表現によって定義されている文字パターンに一致するすべての対象を置換文字列に置き換えます。MatchEvaluator デリゲートは、一致する対象が見つかるたびに置換を評価するために呼び出されます。

名前空間: System.Text.RegularExpressions
アセンブリ: System (system.dll 内)
構文構文

Public Shared Function Replace
 ( _
    input As String, _
    pattern As String, _
    evaluator As MatchEvaluator _
) As String
Dim input As String
Dim pattern As String
Dim evaluator As MatchEvaluator
Dim returnValue As String

returnValue = Regex.Replace(input, pattern, evaluator)
public static string Replace
 (
    string input,
    string pattern,
    MatchEvaluator evaluator
)
public:
static String^ Replace (
    String^ input, 
    String^ pattern, 
    MatchEvaluator^ evaluator
)
public static String Replace (
    String input, 
    String pattern, 
    MatchEvaluator evaluator
)
public static function Replace
 (
    input : String, 
    pattern : String, 
    evaluator : MatchEvaluator
) : String

パラメータ

input

変更対象の文字列。

pattern

一致させる正規表現パターン。

evaluator

置換を 1 つずつ評価する MatchEvaluator。

戻り値
変更後の文字列。

解説解説

MatchEvaluator 型は、入力として単一の Match を取得し、文字列を返すデリゲートです。次のように宣言します。

public delegate String RegexMatchEvaluator(Match match);

このデリゲートは、一致する対象が置換中に見つかるたびに呼び出されます。

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

Regex.Replace メソッド (String, MatchEvaluator, Int32, Int32)

入力文字列の指定した文字位置から開始し、Regex コンストラクタで指定したパターンに一致する対象を指定数だけ置換文字列で置き換えます。MatchEvaluator デリゲートは、一致する対象が見つかるたびに置換を評価するために呼び出されます。

名前空間: System.Text.RegularExpressions
アセンブリ: System (system.dll 内)
構文構文

Public Function Replace ( _
    input As String, _
    evaluator As MatchEvaluator, _
    count As Integer, _
    startat As Integer _
) As String
Dim instance As Regex
Dim input As String
Dim evaluator As MatchEvaluator
Dim count As Integer
Dim startat As Integer
Dim returnValue As String

returnValue = instance.Replace(input, evaluator, count, startat)
public string Replace (
    string input,
    MatchEvaluator evaluator,
    int count,
    int startat
)
public:
String^ Replace (
    String^ input, 
    MatchEvaluator^ evaluator, 
    int count, 
    int startat
)
public String Replace (
    String input, 
    MatchEvaluator evaluator, 
    int count, 
    int startat
)
public function Replace (
    input : String, 
    evaluator : MatchEvaluator, 
    count : int, 
    startat : int
) : String

パラメータ

input

変更対象の文字列。

evaluator

置換を 1 つずつ評価する MatchEvaluator。

count

置換を行う最大回数。

startat

入力文字列中で検索を開始する文字位置。

戻り値
変更後の文字列。

解説解説

MatchEvaluator 型は、入力として単一の Match を取得し、文字列を返すデリゲートです。次のように宣言します。

public delegate String RegexMatchEvaluator(Match match);

このデリゲートは、一致する対象が置換中に見つかるたびに呼び出されます。

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

Regex.Replace メソッド (String, String, MatchEvaluator, RegexOptions)

最初の文字から検索を開始し、指定した正規表現によって定義されている文字パターンに一致するすべての対象を置換文字列に置き換えます。オプションを指定して、一致する動作を変更できます。また、一致する対象が見つかるたびに、置換を評価する MatchEvaluator デリゲートが呼び出されます。

名前空間: System.Text.RegularExpressions
アセンブリ: System (system.dll 内)
構文構文

Public Shared Function Replace
 ( _
    input As String, _
    pattern As String, _
    evaluator As MatchEvaluator, _
    options As RegexOptions _
) As String
Dim input As String
Dim pattern As String
Dim evaluator As MatchEvaluator
Dim options As RegexOptions
Dim returnValue As String

returnValue = Regex.Replace(input, pattern, evaluator, options)
public static string Replace
 (
    string input,
    string pattern,
    MatchEvaluator evaluator,
    RegexOptions options
)
public:
static String^ Replace (
    String^ input, 
    String^ pattern, 
    MatchEvaluator^ evaluator, 
    RegexOptions options
)
public static String Replace (
    String input, 
    String pattern, 
    MatchEvaluator evaluator, 
    RegexOptions options
)
public static function Replace
 (
    input : String, 
    pattern : String, 
    evaluator : MatchEvaluator, 
    options : RegexOptions
) : String

パラメータ

input

変更対象の文字列。

pattern

一致させる正規表現パターン。

evaluator

置換を 1 つずつ評価する MatchEvaluator。

options

ビットごとの OR 演算による RegexOption 列挙値の組み合わせ。

戻り値
変更後の文字列。

解説解説

MatchEvaluator 型は、入力として単一の Match を取得し、文字列を返すデリゲートです。次のように宣言します。

public delegate String RegexMatchEvaluator(Match match);

このデリゲートは、一致する対象が置換中に見つかるたびに呼び出されます。

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

Regex.Replace メソッド

正規表現によって定義されている文字パターンに一致するすべての対象を指定の置換文字列に置き換えます。
オーバーロードの一覧オーバーロードの一覧

名前 説明
Regex.Replace (String, MatchEvaluator) Regex コンストラクタで指定された正規表現によって定義されている文字列パターンに一致するすべての対象を置き換えます。MatchEvaluator デリゲートは、一致する対象が見つかるたびに置換を評価するために呼び出されます。

.NET Compact Framework によってサポートされています。

Regex.Replace (String, String) 入力文字列の最初の文字から検索を開始し、指定した正規表現パターンに一致する対象をすべて置換文字列で置き換えます。

.NET Compact Framework によってサポートされています。

Regex.Replace (String, MatchEvaluator, Int32) 入力文字列の最初の文字から検索を開始し、Regex コンストラクタで指定した正規表現によって定義されているパターンと一致する対象を指定数だけ置換文字列で置き換えます。MatchEvaluator デリゲートは、一致する対象が見つかるたびに置換を評価するために呼び出されます。

.NET Compact Framework によってサポートされています。

Regex.Replace (String, String, Int32) 入力文字列の最初の文字から検索を開始し、Regex コンストラクタで指定した正規表現で定義されているパターンに一致する対象を指定数だけ置換文字列で置き換えます。

.NET Compact Framework によってサポートされています。

Regex.Replace (String, String, MatchEvaluator) 最初の文字から検索を開始し、正規表現によって定義されている文字パターンに一致するすべての対象を置換文字列に置き換えます。MatchEvaluator デリゲートは、一致する対象が見つかるたびに置換を評価するために呼び出されます。

.NET Compact Framework によってサポートされています。

Regex.Replace (String, String, String) 入力文字列の最初の文字から検索を開始し、正規表現によって定義されている文字パターンに一致するすべての対象を置換文字列で置き換えます。

.NET Compact Framework によってサポートされています。

Regex.Replace (String, MatchEvaluator, Int32, Int32) 入力文字列の指定した文字位置から開始し、Regex コンストラクタで指定したパターンに一致する対象を指定数だけ置換文字列で置き換えます。MatchEvaluator デリゲートは、一致する対象が見つかるたびに置換を評価するために呼び出されます。

.NET Compact Framework によってサポートされています。

Regex.Replace (String, String, Int32, Int32) 入力文字列の指定した文字位置から検索を開始し、Regex コンストラクタで指定した正規表現によって定義されている入力文字列内のパターンに一致する対象を指定数だけ指定の置換文字列で置き換えます。

.NET Compact Framework によってサポートされています。

Regex.Replace (String, String, MatchEvaluator, RegexOptions) 最初の文字から検索を開始し、指定した正規表現によって定義されている文字パターンに一致するすべての対象を置換文字列に置き換えます。オプションを指定して、一致する動作を変更できます。また、一致する対象が見つかるたびに、置換を評価する MatchEvaluator デリゲートが呼び出されます。

.NET Compact Framework によってサポートされています。

Regex.Replace (String, String, String, RegexOptions) 入力文字列内の最初の文字から検索を開始し、指定した正規表現によって定義されているパターンに一致するすべての対象を指定の置換文字列で置き換えます。一致の動作を変更するオプションを指定できます。

.NET Compact Framework によってサポートされています。

参照参照

関連項目

Regex クラス
Regex メンバ
System.Text.RegularExpressions 名前空間



英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

「Regex.Replace」の関連用語

Regex.Replaceのお隣キーワード

   

英語⇒日本語
日本語⇒英語
   



Regex.Replaceのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS