.NET Framework 4 Character Escapes Updated: May 2010 The backslash (\) in a regular expression indicates one of the following: The character that follows it is a special character, as shown in the table in the following section. For example, \b is an anchor that indicates that a regular expression match should begin on a word boundary, \t represents a tab, and \x020 represents a space. A character that otherwise would be interpreted as an unescaped language construct should be interpreted literally. For example, a brace ({) begins the definition of a quantifier, but a backslash followed by a brace (\{) indicates that the regular expression engine should match the brace. Similarly, a single backslash marks the beginning of an escaped language construct, but two backslashes (\\) indicate that the regular expression engine should match the backslash.
Note |
|---|
Character escapes are recognized in regular expression patterns but not in replacement patterns. |

Character Escapes in the .NET Framework
The following table lists the character escapes supported by regular expressions in the .NET Framework. Character or sequence | Description |
|---|
All characters except for the following: . $ ^ { [ ( | ) * + ? \ | These characters have no special meanings in regular expressions; they match themselves. | \a | Matches a bell (alarm) character, \u0007. | \b | In a [character_group] character class, matches a backspace, \u0008. (See Character Classes.) Outside a character class, \b is an anchor that matches a word boundary. (See Anchors in Regular Expressions.) | \t | Matches a tab, \u0009. | \r | Matches a carriage return, \u000D. Note that \r is not equivalent to the newline character, \n. | \v | Matches a vertical tab, \u000B. | \f | Matches a form feed, \u000C. | \n | Matches a new line, \u000A. | \e | Matches an escape, \u001B. | \nnn | Matches an ASCII character, where nnn consists of up to three digits that represent the octal character code. For example, \040 represents a space character. However, this construct is interpreted as a backreference if it has only one digit (for example, \2) or if it corresponds to the number of a capturing group. (See Backreference Constructs.) | \xnn | Matches an ASCII character, where nn is a two-digit hexadecimal character code. | \cX | Matches an ASCII control character, where X is the letter of the control character. For example, \cC is CTRL-C. | \unnnn | Matches a Unicode character, where nnnn is a four-digit hexadecimal code point. NoteThe Perl 5 character escape that is used to specify Unicode is not supported by the .NET Framework. The Perl 5 character escape has the form \x{####…}, where ####… is a series of hexadecimal digits. Instead, use \unnnn. | \ | When followed by a character that is not recognized as an escaped character, matches that character. For example, \* matches an asterisk (*) and is the same as \x2A. |

An Example
The following example illustrates the use of character escapes in a regular expression. It parses a string that contains the names of the world's largest cities and their populations in 2009. Each city name is separated from its population by a tab (\t) or a vertical bar (| or \u007c). Individual cities and their populations are separated from each other by a carriage return and line feed.
Imports System.Text.RegularExpressions
Module Example
Public Sub Main()
Dim delimited As String = "\G(.+)[\t\u007c](.+)\r?\n"
Dim input As String = "Mumbai, India|13,922,125" + vbCrLf + _
"Shanghai, China" + vbTab + "13,831,900" + vbCrLf + _
"Karachi, Pakistan|12,991,000" + vbCrLf + _
"Dehli, India" + vbTab + "12,259,230" + vbCrLf + _
"Istanbul, Turkey|11,372,613" + vbCrLf
Console.WriteLine("Population of the World's Largest Cities, 2009")
Console.WriteLine()
Console.WriteLine("{0,-20} {1,10}", "City", "Population")
Console.WriteLine()
For Each match As Match In Regex.Matches(input, delimited)
Console.WriteLine("{0,-20} {1,10}", match.Groups(1).Value, _
match.Groups(2).Value)
Next
End Sub
End Module
' The example displays the following output:
' Population of the World's Largest Cities, 2009
'
' City Population
'
' Mumbai, India 13,922,125
' Shanghai, China 13,831,900
' Karachi, Pakistan 12,991,000
' Dehli, India 12,259,230
' Istanbul, Turkey 11,372,613
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string delimited = @"\G(.+)[\t\u007c](.+)\r?\n";
string input = "Mumbai, India|13,922,125\t\n" +
"Shanghai, China\t13,831,900\n" +
"Karachi, Pakistan|12,991,000\n" +
"Dehli, India\t12,259,230\n" +
"Istanbul, Turkey|11,372,613\n";
Console.WriteLine("Population of the World's Largest Cities, 2009");
Console.WriteLine();
Console.WriteLine("{0,-20} {1,10}", "City", "Population");
Console.WriteLine();
foreach (Match match in Regex.Matches(input, delimited))
Console.WriteLine("{0,-20} {1,10}", match.Groups[1].Value,
match.Groups[2].Value);
}
}
// The example displyas the following output:
// Population of the World's Largest Cities, 2009
//
// City Population
//
// Mumbai, India 13,922,125
// Shanghai, China 13,831,900
// Karachi, Pakistan 12,991,000
// Dehli, India 12,259,230
// Istanbul, Turkey 11,372,613
The regular expression \G(.+)[\t|\u007c](.+)\r?\n is interpreted as shown in the following table. Pattern | Description |
|---|
\G | Begin the match where the last match ended. | (.+) | Match any character one or more times. This is the first capturing group. | [\t\u007c] | Match a tab (\t) or a vertical bar (|). | (.+) | Match any character one or more times. This is the second capturing group. | \r? \n | Match zero or one occurrence of a carriage return followed by a new line. |

See Also

Change History
Date | History | Reason |
|---|
May 2010
| Corrected the regular expression pattern. |
Customer feedback.
|
|
.NET Framework 4 文字のエスケープ 更新 : 2010 年 5 月 正規表現の円記号 (\) は、次のいずれかを示します。 後に続く文字が、次のセクションの表に示す特殊文字であること。 たとえば、\b は、正規表現の一致がワード境界から開始する必要があることを示すアンカーであり、\t はタブを表します。\x020 は空白を表します。 エスケープされていない言語構成要素として解釈される文字は、文字どおりに解釈する必要があること。 たとえば、中かっこ ({) は量指定子の定義を開始しますが、円記号とそれに続く中かっこ (\{) は、正規表現エンジンで中かっこに一致させる必要があることを示します。 同様に、1 つの円記号はエスケープされた言語構成要素の開始を示しますが、2 つの円記号 (\\) は、正規表現エンジンで円記号に一致させる必要があることを示します。
メモ |
|---|
文字エスケープは、正規表現では認識されますが、置換パターンでは認識されません。 |

.NET Framework の文字エスケープ
次の表は、.NET Framework の正規表現でサポートされている文字エスケープの一覧です。 文字または文字シーケンス | 説明 |
|---|
次の文字を除くすべての文字: . $ ^ { [ ( | ) * + ? \ | これらの文字は、正規表現で特殊な意味を持たず、その文字自体と一致します。 | \a | ビープ音 (アラーム) 文字の \u0007 と一致します。 | \b | [文字グループ] 文字クラスでは、バックスペースの \u0008 と一致します (「文字クラス」を参照)。 文字クラスの外部では、\b はワード境界と一致するアンカーです (「正規表現のアンカー」を参照)。 | \t | タブの \u0009 と一致します。 | \r | キャリッジ リターンの \u000D と一致します。 \r の機能は \n という改行文字と同じではありません。 | \v | 垂直タブの \u000B と一致します。 | \f | フォーム フィードの \u000C と一致します。 | \n | 改行文字の \u000A と一致します。 | \e | エスケープ文字の \u001B と一致します。 | \nnn | ASCII 文字と一致します。nnn は、8 進文字コードを表す 3 桁までの桁で構成されます。 たとえば、\040 は、空白文字を表します。 ただし、この構成要素は、1 桁のみの場合 (たとえば、\2 など)、またはキャプチャ グループの番号に対応する場合には前方参照として解釈されます。 (「前方参照構成体」を参照)。 | \xnn | ASCII 文字と一致します。nn は 2 桁の 16 進文字コードです。 | \cX | ASCII の制御文字と一致します。X は制御文字です。 たとえば、\cC は CTRL-C です。 | \unnnn | Unicode 文字と一致します。nnnn は 4 桁の 16 進コード ポイントです。 メモ.NET Framework では、Unicode を指定するために使用する Perl5 の文字エスケープはサポートされません。 Perl 5 の文字エスケープは \x{####…} の形式です。ここで、####… は一連の 16 進数字です。 代わりに \unnnn を使用してください。 | \ | エスケープ文字として認識されない文字が後ろに付いている場合は、その文字と一致します。 たとえば、\* はアスタリスク (*) と一致し、\x2A と同じです。 |

例
正規表現の文字エスケープの使用例を次に示します。 2009 年の世界最大規模の都市の名前と人口を含む文字列を解析します。 個々の都市の名前は、タブ (\t) または縦棒 (| または \u007c) で人口と区切られています。 個々の都市とその人口は、復帰とライン フィードで互いに区切られています。
Imports System.Text.RegularExpressions
Module Example
Public Sub Main()
Dim delimited As String = "\G(.+)[\t\u007c](.+)\r?\n"
Dim input As String = "Mumbai, India|13,922,125" + vbCrLf + _
"Shanghai, China" + vbTab + "13,831,900" + vbCrLf + _
"Karachi, Pakistan|12,991,000" + vbCrLf + _
"Dehli, India" + vbTab + "12,259,230" + vbCrLf + _
"Istanbul, Turkey|11,372,613" + vbCrLf
Console.WriteLine("Population of the World's Largest Cities, 2009")
Console.WriteLine()
Console.WriteLine("{0,-20} {1,10}", "City", "Population")
Console.WriteLine()
For Each match As Match In Regex.Matches(input, delimited)
Console.WriteLine("{0,-20} {1,10}", match.Groups(1).Value, _
match.Groups(2).Value)
Next
End Sub
End Module
' The example displays the following output:
' Population of the World's Largest Cities, 2009
'
' City Population
'
' Mumbai, India 13,922,125
' Shanghai, China 13,831,900
' Karachi, Pakistan 12,991,000
' Dehli, India 12,259,230
' Istanbul, Turkey 11,372,613
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string delimited = @"\G(.+)[\t\u007c](.+)\r?\n";
string input = "Mumbai, India|13,922,125\t\n" +
"Shanghai, China\t13,831,900\n" +
"Karachi, Pakistan|12,991,000\n" +
"Dehli, India\t12,259,230\n" +
"Istanbul, Turkey|11,372,613\n";
Console.WriteLine("Population of the World's Largest Cities, 2009");
Console.WriteLine();
Console.WriteLine("{0,-20} {1,10}", "City", "Population");
Console.WriteLine();
foreach (Match match in Regex.Matches(input, delimited))
Console.WriteLine("{0,-20} {1,10}", match.Groups[1].Value,
match.Groups[2].Value);
}
}
// The example displyas the following output:
// Population of the World's Largest Cities, 2009
//
// City Population
//
// Mumbai, India 13,922,125
// Shanghai, China 13,831,900
// Karachi, Pakistan 12,991,000
// Dehli, India 12,259,230
// Istanbul, Turkey 11,372,613
正規表現 \G(.+)[\t|\u007c](.+)\r?\n の解釈を次の表に示します。 パターン | 説明 |
|---|
\G | 最後の一致の終了位置から照合を開始します。 | (.+) | 任意の文字と 1 回以上、一致します。 これが最初のキャプチャ グループです。 | [\t\u007c] | タブ (\t) または縦棒 (|) と一致します。 | (.+) | 任意の文字と 1 回以上、一致します。 これが 2 番目のキャプチャ グループです。 | \r? \n | 復帰とそれに続く改行に、0 回または 1 回一致します。 |

参照

履歴の変更
日付 | 履歴 | 理由 |
|---|
2010 年 5 月
| 正規表現パターンを修正しました。 |
カスタマー フィードバック
|
|