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 名前空間


このページでは「.NET Framework クラス ライブラリ リファレンス」からRegex.Replace メソッドを検索した結果を表示しています。
Weblioに収録されているすべての辞書からRegex.Replace メソッドを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からRegex.Replace メソッド を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「Regex.Replace メソッド」の関連用語

Regex.Replace メソッドのお隣キーワード
検索ランキング

   

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



Regex.Replace メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

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

©2024 GRAS Group, Inc.RSS