• 検索結果がありません。

正規表現詳細

N/A
N/A
Protected

Academic year: 2021

シェア "正規表現詳細"

Copied!
12
0
0

読み込み中.... (全文を見る)

全文

(1)

■ 正規表現言語要素 ■ .NET Framework SDK には、包括的な正規表現ツールのセットが用意されて居り、其のツールを使用 する事で文字列の作成、比較、修正を効率的に行う。亦、大量のテキストやデータを素早く解析し、テ キストパターンの検索、削除、置換を行う事が出来る。 此処では、正規表現を定義する一連の文字、演算子、及び、構成体に付いて説明する。 ■ 置換 .NET Framework 2 置換構成体は、置換パターン丈で使用出来る。正規表現で同様の機能を使用するには、前方参照(¥1 等)を使用する。前方参照の詳細に付いては、「前方参照」及び「前方参照構成体」を参照され度い。 エスケープ文字と置換構成体は、置換パターンで認識される特殊な構成体で有る。次のセクションで説 明する総ての構文上の構成体は、正規表現で丈使用出来る。置換パターンでは認識されない。例えば、 置換パターン a*${txt}b の場合、"a*" の後に txt キャプチャグループに一致する部分文字列、更に、文 字列 "b"(指定されて居る場合)が続く様な文字列を挿入する。*文字は、置換パターンではメタ文字と 仕て認識されない。同様に、$パターンは、正規表現一致パターンでは認識されない。正規表現では、$は 文字列の末尾を示す。 名前付き置換パターン、及び、番号付き置換パターンを定義する方法を下記の表に示す。 文字 説明 $ number グループ番号 number(10 進数)と一致した最後の部分文字列に置換される。 ${ name } (?<name> ) グループと一致した最後の部分文字列に置換される。 $$ 単一の "$" リテラルに置換される。 $& 一致したパターン全体と同じパターンに置換される。 $` 一致した場所より前に有る入力文字列の総てに置換される。 $' 一致した場所より後に有る入力文字列の総てに置換される。 $+ キャプチャされた最後のグループに置換される。 $_ 入力文字列全体に置換される。 .NET Framework 4 置換は、置換パターン内で而巳認識される言語要素で有る。置換では、正規表現パターンを使用して、 入力文字列内の一致するテキストを置換するテキストの全体や一部を定義する。置換パターンは、1 個 以 上 の 置 換 と リ テ ラ ル 文 字 で 構 成 さ れ る 。 置 換 パ タ ー ン は 、replacement パ ラ メ ー タ を 持 つ Regex.Replace メソッドのオーバーロードと Match.Result メソッドに対して用意されて居る。メソッ ドは、一致するパターンを、replacement パラメータで定義されて居るパターンで置換する。 .NET Framework では、下記の表に示す置換要素が定義されて居る。

正規

規表

表現

(2)

Substitution 解説 $数 番号で識別されるキャプチャグループに一致する最後の部分文字列を置換文字列に含 める。番号は10 進値で有る(⇒ 番号付きグループの置換)。 ${<名前>} (?<名前> ) で指定された名前付きグループに一致する最後の部分文字列を置換文字 列に含める(⇒ 名前付きグループの置換)。 $$ 置換文字列に1 個の "$" リテラルを含める(⇒ "$" 文字の置換)。 $& 一致した文字列全体のコピーを置換文字列に含める(⇒ 一致した文字列全体の置換)。 $` 一致した場所より前に有る入力文字列のテキスト総てを置換文字列に含める(⇒ 一致 した文字列より前に有るテキストの置換)。 $' 一致した場所より後に有る入力文字列のテキスト総てを置換文字列に含める(⇒ 一致 した文字列より後に有るテキストの置換)。 $+ 最後にキャプチャされたグループを置換文字列に含める(⇒ キャプチャされた最後の グループの置換)。 $_ 入力文字列全体を置換文字列に含める(⇒ 入力文字列全体の置換)。 置換要素と置換パターン 置換構成体は、置換パターンで認識される特殊な構成体で有る。文字エスケープやピリオド( . )等、 任意の文字に一致する他の正規表現言語要素は孰れもサポートされて居ない。同様に、置換言語要素は 置換パターン内で而巳認識され、正規表現パターン内では有効では無い。 正規表現パターンと置換の両方に使用出来る文字は$文字丈だが、此の文字の意味はコンテキストに依 って異なる。正規表現パターンでは、$は文字列の末尾に一致するアンカーで有る。置換パターンでは、 $は置換の先頭を示す。 ※ 正規表現の中で置換パターンに似た機能を利用するには、前方参照を使用する(⇒ 前方参照構成体)。 番号付きグループの置換 $番号言語要素は、番号キャプチャグループに一致する最後の部分文字列を置換文字列に含める。番号 は、キャプチャグループのインデックスで有る。例えば、置換パターン$1 は、一致した部分文字列がキ ャプチャされた最初のグループに置き換えられる事を示す(⇒ グループ化構成体)。 (?<名前>) 構文を使用して名前が明示的に割り当てられて居ないキャプチャグループには、1 から開始 する番号が、左から右の順に割り当てられる。名前付きグループにも、最後の名前の無いグループのイ ンデックスよりも1 個大きい数値から開始する番号が、左から右へと順に割り当てられる。例えば、正 規表現 (¥w)(?<digit>¥d) では、digit と謂う名前付きグループのインデックスは 2 で有る。 番号が、正規表現パターンで定義される有効なキャプチャグループを指定して居ない場合は、$番号が、 一致した各文字列の置換に使用されるリテラル文字列シーケンスと仕て解釈される。 下記の例では、$番号の置換を使用して、10 進値から通貨記号を削除して居る。期首と期末の金銭的価 値、通貨記号を削除し、2 個の最も一般的な小数点の記号を認識して居る( . と , )。 Visual Basic Imports System.Text.RegularExpressions Module Example

Public Sub Main( )

Dim pattern As String = "¥p{Sc}*(¥s?¥d+[.,]?¥d*)¥p{Sc}*" Dim replacement As String = "$1"

(3)

Dim input As String = "$16.32 12.19 £16.29 ?18.29 ?18,29" Dim result As String = Regex.Replace(input, pattern, replacement) Console.WriteLine(result)

End Sub End Module

' The example displays the following output: ' 16.32 12.19 16.29 18.29 18,29 C#

using System;

using System.Text.RegularExpressions; public class Example

{

public static void Main() {

string pattern = @"¥p{Sc}*(¥s?¥d+[.,]?¥d*)¥p{Sc}*"; string replacement = "$1";

string input = "$16.32 12.19 £16.29 ?18.29 ?18,29"; string result = Regex.Replace(input, pattern, replacement); Console.WriteLine(result);

} }

// The example displays the following output: // 16.32 12.19 16.29 18.29 18,29 正規表現パターン ¥p{Sc}*(¥s? ¥d+[.,]? ¥d*)¥p{Sc}* は、下記の表の様に定義されて居る。 パターン 解説 ¥p{Sc}* 0 個以上の通貨記号文字と一致する。 ¥s? 0 個か 1 個の空白文字と一致する。 ¥d+ 1 個以上の 10 進数と一致する。 [.,]? 0 個か 1 個のピリオドかコンマと一致する。 ¥d* 0 個以上の 10 進数と一致する。 (¥s? ¥d+[.,]? ¥d*) 空白の後に 1 個以上の 10 進数、0 個か 1 個のピリオドかコンマ、更に 0 個以上 の10 進数が続くパターンに一致する。此れが最初のキャプチャグループで有る。 置換パターンは$1 で有る為、Regex.Replace メソッドを呼び出すと、一致する 部分文字列全体が此のキャプチャされたグループに置き換えられる。 名前付きのグループの置換 ${名前} 言語要素は、名前キャプチャグループに一致する最後の部分文字列を置換する。此処で、名前 は (?<名前>) 言語要素で定義されて居るキャプチャグループの名前で有る(⇒ グループ化構成体)。 名前が、正規表現パターンで定義される有効な名前付きキャプチャグループを指定して居ない場合は、 ${名前}が、一致した各文字列の置換に使用されるリテラル文字列シーケンスと仕て解釈される。 下記の例では、${名前} の置換を使用して、10 進値から通貨記号を削除して居る。期首と期末の金銭的 価値、通貨記号を削除し、2 個の最も一般的な小数点の記号を認識して居る( . と , )。 Visual Basic Imports System.Text.RegularExpressions

(4)

Module Example Public Sub Main( )

Dim pattern As String = "¥p{Sc}*(?<amount>¥s?¥d+[.,]?¥d*)¥p{Sc}*" Dim replacement As String = "${amount}"

Dim input As String = "$16.32 12.19 £16.29 ?18.29 ?18,29" Dim result As String = Regex.Replace(input, pattern, replacement) Console.WriteLine(result)

End Sub End Module

' The example displays the following output: ' 16.32 12.19 16.29 18.29 18,29 C#

using System;

using System.Text.RegularExpressions; public class Example

{

public static void Main() {

string pattern = @"¥p{Sc}*(?<amount>¥s?¥d+[.,]?¥d*)¥p{Sc}*"; string replacement = "${amount}";

string input = "$16.32 12.19 £16.29 ?18.29 ?18,29"; string result = Regex.Replace(input, pattern, replacement); Console.WriteLine(result);

} }

// The example displays the following output: // 16.32 12.19 16.29 18.29 18,29 正規表現パターン ¥p{Sc}*(?<amount>¥s? ¥d[.,]? ¥d*)¥p{Sc}* は、下記の表の様に定義されて居る。 パターン 解説 ¥p{Sc}* 0 個以上の通貨記号文字と一致する。 ¥s? 0 個又は 1 個の空白文字と一致する。 ¥d+ 1 個以上の 10 進数と一致する。 [.,]? 0 個か 1 個のピリオドかコンマと一致する。 ¥d* 0 個以上の 10 進数と一致する。 (?<amount>¥s? ¥d[.,]? ¥d*) 空白の後に 1 個以上の 10 進数、0 個か 1 個のピリオドかコンマ、更 に0 個以上の 10 進数が続くパターンに一致する。此れは、amount と謂う名前のキャプチャグループで有る。置換パターンは${amount} で有る為、Regex.Replace メソッドを呼び出すと、一致する部分文字 列全体が此のキャプチャされたグループに置き換えられる。 "$" 文字の置換 $$の置換は、リテラル文字 "$" を置換文字列に挿入する。 下記の例では、NumberFormatInfo オブジェクトを使用して、現在のカルチャの通貨記号と、通貨文字 列内での其の配置を決定する。次に、正規表現パターンと置換パターンの両方を動的に構築する。現在 en-US カルチャが使用されて居るコンピューターで此の例を実行すると、¥b(¥d+)(¥.(¥d+))? と謂う正

(5)

規表現及び $$ $1$2 と謂う置換パターンが生成される。置換パターンは、一致するテキストを、通貨 記号と空白に続いて、キャプチャされた最初と2 番目のグループで置換する。 Visual Basic Imports System.Globalization Imports System.Text.RegularExpressions Module Example

Public Sub Main( )

' Define array of decimal values.

Dim values() As String = { "16.35", "19.72", "1234", "0.99"}

' Determine whether currency precedes (True) or follows (False) number. Dim precedes As Boolean = _

(NumberFormatInfo.CurrentInfo.CurrencyPositivePattern Mod 2 = 0) ' Get decimal separator.

Dim cSeparator As String = NumberFormatInfo.CurrentInfo.CurrencyDecimalSeparator ' Get currency symbol.

Dim symbol As String = NumberFormatInfo.CurrentInfo.CurrencySymbol ' If symbol is a "$", add an extra "$".

If symbol = "$" Then symbol = "$$"

' Define regular expression pattern and replacement string. Dim pattern As String = "¥b(¥d+)(" + cSeparator + "(¥d+))?" Dim replacement As String = "$1$2"

replacement = If(precedes, symbol + " " + replacement, replacement + " " + symbol) For Each value In values

Console.WriteLine("{0} --> {1}", value, Regex.Replace(value, pattern, replacement)) Next

End Sub End Module

' The example displays the following output: ' 16.35 --> $ 16.35 ' 19.72 --> $ 19.72 ' 1234 --> $ 1234 ' 0.99 --> $ 0.99 C# using System; using System.Globalization; using System.Text.RegularExpressions; public class Example

{

public static void Main( ) {

// Define array of decimal values.

string[] values= { "16.35", "19.72", "1234", "0.99"};

// Determine whether currency precedes (True) or follows (False) number.

bool precedes = NumberFormatInfo.CurrentInfo.CurrencyPositivePattern % 2 == 0; // Get decimal separator.

string cSeparator = NumberFormatInfo.CurrentInfo.CurrencyDecimalSeparator; // Get currency symbol.

string symbol = NumberFormatInfo.CurrentInfo.CurrencySymbol; // If symbol is a "$", add an extra "$".

(6)

// Define regular expression pattern and replacement string. string pattern = @"¥b(¥d+)(" + cSeparator + @"(¥d+))?"; string replacement = "$1$2";

replacement = precedes ? symbol + " " + replacement : replacement + " " + symbol; foreach (string value in values)

Console.WriteLine("{0} --> {1}", value, Regex.Replace(value, pattern, replacement)); }

}

// The example displays the following output: // 16.35 --> $ 16.35 // 19.72 --> $ 19.72 // 1234 --> $ 1234 // 0.99 --> $ 0.99 正規表現パターン ¥b(¥d+)(¥.(¥d+))? は、下記の表に示す様に定義されて居る。 パターン 解説 ¥b ワード境界の先頭から照合を開始する。 (¥d+) 1 個以上の 10 進数と一致する。此れが最初のキャプチャグループで有る。 ¥. ピリオド(桁区切り記号)と一致する。 (¥d+) 1 個以上の 10 進数と一致する。此れが 3 番目のキャプチャグループで有る。 (¥.(¥d+))? ピリオドの後に 1 個以上の 10 進数が続くパターンの 0 回か 1 回の出現と一致する。此 れが2 番目のキャプチャグループで有る。 一致した文字列全体の置換 $&の置換は、一致した文字列全体を置換文字列に含める。通常は、一致した文字列の先頭か末尾に部分 文字列を追加する為に使用される。例えば、($&) と謂う置換パターンは、一致した各文字列の先頭と 末尾に括弧を追加する。一致する文字列が無い場合、$&の置換は無効で有る。 $&の置換を使用して、文字列配列に格納されて居る書籍タイトルの先頭と末尾に引用符を追加する例を 下記に示す。 Visual Basic Imports System.Text.RegularExpressions Module Example

Public Sub Main( )

Dim pattern As String = "^(¥w+¥s?)+$"

Dim titles() As String = { "A Tale of Two Cities", _

"The Hound of the Baskervilles", _

"The Protestant Ethic and the Spirit of Capitalism", _ "The Origin of Species" }

Dim replacement As String = """$&""" For Each title As String In titles

Console.WriteLine(Regex.Replace(title, pattern, replacement)) Next

End Sub End Module

' The example displays the following output: ' "A Tale of Two Cities"

(7)

' "The Hound of the Baskervilles"

' "The Protestant Ethic and the Spirit of Capitalism" ' "The Origin of Species"

C#

using System;

using System.Text.RegularExpressions; public class Example

{

public static void Main() {

string pattern = @"^(¥w+¥s?)+$"; string[] titles = { "A Tale of Two Cities",

"The Hound of the Baskervilles",

"The Protestant Ethic and the Spirit of Capitalism", "The Origin of Species" };

string replacement = "¥"$&¥""; foreach (string title in titles)

Console.WriteLine(Regex.Replace(title, pattern, replacement)); }

}

// The example displays the following output: // "A Tale of Two Cities"

// "The Hound of the Baskervilles"

// "The Protestant Ethic and the Spirit of Capitalism" // "The Origin of Species"

正規表現パターン ^(¥w+¥s?)+$ は、下記の表に示す様に定義されて居る。 パターン 解説 ^ 入力文字列の先頭から照合を開始する。 (¥w+¥s?)+ 1 個以上の単語文字の後に 0 個か 1 個の空白文字が 1 回以上続くパターンに一致する。 $ 入力文字列の末尾と一致する。 "$&" と謂う置換パターンは、各一致文字列の先頭と末尾にリテラルの一重引用符を追加する。 一致した文字列より前に有るテキストの置換 $` の置換は、一致した場所より前に有る入力文字列全体で一致した文字列を置換する。つまり、一致し た場所迄の入力文字列を複製し、一致したテキストを削除する。結果文字列では、一致したテキストに 続くテキストは変更されない。入力文字列に複数の一致文字列が有る場合、置換テキストは、テキスト が前の一致で置換された文字列からではなく、元の入力文字列から派生する(具体的な例を下記に示す)。 一致する文字列が無い場合、$` の置換は無効で有る。 下記の例では、正規表現パターン¥d+を使用して、入力文字列内の 1 個以上の 10 進数のシーケンスを 照合する。置換文字列$`は、此等の数字を、一致文字列より前に有るテキストで置換する。 Visual Basic Imports System.Text.RegularExpressions Module Example

(8)

Dim input As String = "aa1bb2cc3dd4ee5" Dim pattern As String = "¥d+"

Dim substitution As String = "$`" Console.WriteLine("Matches:")

For Each match As Match In Regex.Matches(input, pattern)

Console.WriteLine(" {0} at position {1}", match.Value, match.Index) Next

Console.WriteLine("Input string: {0}", input)

Console.WriteLine("Output string: " + Regex.Replace(input, pattern, substitution)) End Sub

End Module

' The example displays the following output: ' Matches: ' 1 at position 2 ' 2 at position 5 ' 3 at position 8 ' 4 at position 11 ' 5 at position 14

' Input string: aa1bb2cc3dd4ee5

' Output string: aaaabbaa1bbccaa1bb2ccddaa1bb2cc3ddeeaa1bb2cc3dd4ee C#

using System;

using System.Text.RegularExpressions; public class Example

{

public static void Main( ) {

string input = "aa1bb2cc3dd4ee5"; string pattern = @"¥d+";

string substitution = "$`"; Console.WriteLine("Matches:");

foreach (Match match in Regex.Matches(input, pattern))

Console.WriteLine(" {0} at position {1}", match.Value, match.Index); Console.WriteLine("Input string: {0}", input);

Console.WriteLine("Output string: " + Regex.Replace(input, pattern, substitution)); }

}

// The example displays the following output: // Matches: // 1 at position 2 // 2 at position 5 // 3 at position 8 // 4 at position 11 // 5 at position 14

// Input string: aa1bb2cc3dd4ee5

// Output string: aaaabbaa1bbccaa1bb2ccddaa1bb2cc3ddeeaa1bb2cc3dd4ee

此の例では、入力文字列 "aa1bb2cc3dd4ee5" に 5 個の一致が含まれて居る。$`の置換に依って、正規 表現エンジンが入力文字列の各一致文字列を何の様に置換するかを、下記の表に示す。

Match 位置 一致した場所より前に有る文字列 結果文字列

(9)

2 5 aa1bb aaaabbaa1bbcc3dd4ee5 3 8 aa1bb2cc aaaabbaa1bbccaa1bb2ccdd4ee5 4 11 aa1bb2cc3dd aaaabbaa1bbccaa1bb2ccddaa1bb2cc3ddee5 5 14 aa1bb2cc3dd4ee aaaabbaa1bbccaa1bb2ccddaa1bb2cc3ddee aa1bb2cc3dd4ee 一致した文字列より後に有るテキストの置換 $' の置換は、一致した場所より後に有る入力文字列全体で一致した文字列を置換する。詰り、一致した 場所より後に有る入力文字列を複製し、一致したテキストを削除する。結果文字列では、一致したテキ ストより前に有るテキストは変更されない。一致する文字列が無い場合、$'の置換は無効で有る。 下記の例では、正規表現パターン ¥d+ を使用して、入力文字列内の 1 個以上の 10 進数のシーケンス を照合する。置換文字列$'は、此等の数字を、一致文字列に続くテキストで置換する。 Visual Basic Imports System.Text.RegularExpressions Module Example

Public Sub Main( )

Dim input As String = "aa1bb2cc3dd4ee5" Dim pattern As String = "¥d+"

Dim substitution As String = "$'" Console.WriteLine("Matches:")

For Each match As Match In Regex.Matches(input, pattern)

Console.WriteLine(" {0} at position {1}", match.Value, match.Index) Next

Console.WriteLine("Input string: {0}", input)

Console.WriteLine("Output string: " + Regex.Replace(input, pattern, substitution)) End Sub

End Module

' The example displays the following output: ' Matches: ' 1 at position 2 ' 2 at position 5 ' 3 at position 8 ' 4 at position 11 ' 5 at position 14

' Input string: aa1bb2cc3dd4ee5

' Output string: aaaabbaa1bbccaa1bb2ccddaa1bb2cc3ddeeaa1bb2cc3dd4ee C#

using System;

using System.Text.RegularExpressions; public class Example

{

public static void Main() {

string input = "aa1bb2cc3dd4ee5"; string pattern = @"¥d+";

string substitution = "$'"; Console.WriteLine("Matches:");

(10)

Console.WriteLine(" {0} at position {1}", match.Value, match.Index); Console.WriteLine("Input string: {0}", input);

Console.WriteLine("Output string: " + Regex.Replace(input, pattern, substitution)); }

}

// The example displays the following output: // Matches: // 1 at position 2 // 2 at position 5 // 3 at position 8 // 4 at position 11 // 5 at position 14

// Input string: aa1bb2cc3dd4ee5

// Output string: aaaabbaa1bbccaa1bb2ccddaa1bb2cc3ddeeaa1bb2cc3dd4ee

此の例では、入力文字列 "aa1bb2cc3dd4ee5" に 5 個の一致が含まれて居る。$'の置換に依って、正規 表現エンジンが入力文字列の各一致文字列を何の様に置換するかを、下記の表に示す。 Match 位置 一致した場所より後に有る文字列 結果文字列 1 2 bb2cc3dd4ee5 aabb2cc3dd4ee5bb2cc3dd4ee5 2 5 cc3dd4ee5 aabb2cc3dd4ee5bbcc3dd4ee5cc3dd4ee5 3 8 dd4ee5 aabb2cc3dd4ee5bbcc3dd4ee5ccdd4ee5dd4ee5 4 11 ee5 aabb2cc3dd4ee5bbcc3dd4ee5ccdd4ee5ddee5ee5 5 14 String.Empty aabb2cc3dd4ee5bbcc3dd4ee5ccdd4ee5ddee5ee キャプチャされた最後のグループの置換 $+ の置換は、キャプチャされた最後のグループで一致した文字列を置換する。キャプチャされたグル ープが無い場合、又は、キャプチャされた最後のグループの値がString.Empty の場合、$+の置換は無 効で有る。 下記の例では、文字列内の重複する単語を識別し、$+の置換を使用して、此等の単語を其の単語 1 個に 置換する。RegexOptions.IgnoreCase オプションを使用すると、大文字と小文字の違いを除いて同一で 有る単語が重複と看做される様に成る。 Visual Basic Imports System.Text.RegularExpressions Module Example

Public Sub Main( )

Dim pattern As String = "¥b(¥w+)¥s¥1¥b" Dim substitution As String = "$+"

Dim input As String = "The the dog jumped over the fence fence."

Console.WriteLine(Regex.Replace(input, pattern, substitution, RegexOptions.IgnoreCase)) End Sub

End Module

' The example displays the following output: ' The dog jumped over the fence.

C#

using System;

(11)

public class Example {

public static void Main() {

string pattern = @"¥b(¥w+)¥s¥1¥b"; string substitution = "$+";

string input = "The the dog jumped over the fence fence.";

Console.WriteLine(Regex.Replace(input, pattern, substitution, RegexOptions.IgnoreCase)); }

}

// The example displays the following output: // The dog jumped over the fence.

正規表現パターン ¥b(¥w+)¥s¥1¥b は、下記の表に示す様に定義されて居る。 パターン 解説 ¥b 単語境界から照合を開始する。 (¥w+) 1 個以上の単語文字に一致する。此れが最初のキャプチャグループで有る。 ¥s 空白文字と一致する。 ¥1 キャプチャされた最初のグループと一致する。 ¥b ワード境界で照合を終了する。 入力文字列全体の置換 $_ の置換は、一致した文字列を入力文字列全体で置換する。詰り、一致したテキストを削除し、一致 したテキストを含む文字列全体で其のテキストを置換する。 下記の例では、入力文字列の1 個以上の 10 進数を照合する。$_ 置換を使用して、此等を入力文字列全 体で置換する。 Visual Basic Imports System.Text.RegularExpressions Module Example

Public Sub Main( )

Dim input As String = "ABC123DEF456" Dim pattern As String = "¥d+"

Dim substitution As String = "$_"

Console.WriteLine("Original string: {0}", input) Console.WriteLine("String with substitution: {0}", _ Regex.Replace(input, pattern, substitution)) End Sub

End Module

' The example displays the following output: ' Original string: ABC123DEF456

' String with substitution: ABCABC123DEF456DEFABC123DEF456 C#

using System;

(12)

public class Example {

public static void Main() {

string input = "ABC123DEF456"; string pattern = @"¥d+";

string substitution = "$_";

Console.WriteLine("Original string: {0}", input); Console.WriteLine("String with substitution: {0}",

Regex.Replace(input, pattern, substitution)); }

}

// The example displays the following output: // Original string: ABC123DEF456

// String with substitution: ABCABC123DEF456DEFABC123DEF456

此の例では、入力文字列 "ABC123DEF456" に 2 個の一致が含まれて居る。$_の置換に依って、正規 表現エンジンが入力文字列の各一致文字列を何の様に置換するかを、下記の表に示す。

Match 位置 Match 結果文字列

1 3 123 ABCABC123DEF456DEF456

参照

関連したドキュメント

この 文書 はコンピューターによって 英語 から 自動的 に 翻訳 されているため、 言語 が 不明瞭 になる 可能性 があります。.. このドキュメントは、 元 のドキュメントに 比 べて

2021] .さらに対応するプログラミング言語も作

②立正大学所蔵本のうち、現状で未比定のパーリ語(?)文献については先述の『請来資料目録』に 掲載されているが

本論文での分析は、叙述関係の Subject であれば、 Predicate に対して分配される ことが可能というものである。そして o

・条例第 37 条・第 62 条において、軽微なものなど規則で定める変更については、届出が不要とされ、その具 体的な要件が規則に定められている(規則第

それに対して現行民法では︑要素の錯誤が発生した場合には錯誤による無効を承認している︒ここでいう要素の錯

このような環境要素は一っの土地の構成要素になるが︑同時に他の上地をも流動し︑又は他の上地にあるそれらと

自然言語というのは、生得 な文法 があるということです。 生まれつき に、人 に わっている 力を って乳幼児が獲得できる言語だという え です。 語の それ自 も、 から