# 規則運算式中的替代建構Alternation Constructs in Regular Expressions

## 以 | 進行的模式比對Pattern Matching with |

| 字元和正字元類別一樣，可以用來比對一些單一字元當中的任一字元。Like the positive character class, the | character can be used to match any one of a number of single characters. 下列範例同時使用正字元類別和透過 | 字元的二選一模式比對，在字串中尋找與單字 "gray" 或 "grey" 相符的項目。The following example uses both a positive character class and either/or pattern matching with the | character to locate occurrences of the words "gray" or "grey" in a string. 在此案例中， | 字元會產生更詳細的規則運算式。In this case, the | character produces a regular expression that is more verbose.

using System;
using System.Text.RegularExpressions;

public class Example
{
public static void Main()
{
// Regular expression using character class.
string pattern1 = @"\bgr[ae]y\b";
// Regular expression using either/or.
string pattern2 = @"\bgr(a|e)y\b";

string input = "The gray wolf blended in among the grey rocks.";
foreach (Match match in Regex.Matches(input, pattern1))
Console.WriteLine("'{0}' found at position {1}",
match.Value, match.Index);
Console.WriteLine();
foreach (Match match in Regex.Matches(input, pattern2))
Console.WriteLine("'{0}' found at position {1}",
match.Value, match.Index);
}
}
// The example displays the following output:
//       'gray' found at position 4
//       'grey' found at position 35
//
//       'gray' found at position 4
//       'grey' found at position 35

Imports System.Text.RegularExpressions

Module Example
Public Sub Main()
' Regular expression using character class.
Dim pattern1 As String = "\bgr[ae]y\b"
' Regular expression using either/or.
Dim pattern2 As String = "\bgr(a|e)y\b"

Dim input As String = "The gray wolf blended in among the grey rocks."
For Each match As Match In Regex.Matches(input, pattern1)
Console.WriteLine("'{0}' found at position {1}", _
match.Value, match.Index)
Next
Console.WriteLine()
For Each match As Match In Regex.Matches(input, pattern2)
Console.WriteLine("'{0}' found at position {1}", _
match.Value, match.Index)
Next
End Sub
End Module
' The example displays the following output:
'       'gray' found at position 4
'       'grey' found at position 35
'
'       'gray' found at position 4
'       'grey' found at position 35


\b 從字緣開始。Start at a word boundary.
gr 比對字元 "gr"。Match the characters "gr".
(a|e) 比對 "a" 或 "e"。Match either an "a" or an "e".
y\b 比對字邊界上的 "y"。Match a "y" on a word boundary.

| 字元也可以用來與多個字元或子運算式執行兩選一的比對，這些字元或運算式可以包含字元常值和規則運算式語言項目的任何組合。The | character can also be used to perform an either/or match with multiple characters or subexpressions, which can include any combination of character literals and regular expression language elements. （字元類別不提供這種功能）。下列範例會使用 | 字元來解壓縮美國社會安全號碼（SSN），這是一種以ddd-dd-dddd或美國雇主識別碼（EIN）格式的9位數數位，是9位數的數位，其格式為dd-ddddddd(The character class does not provide this functionality.) The following example uses the | character to extract either a U.S. Social Security Number (SSN), which is a 9-digit number with the format ddd-dd-dddd, or a U.S. Employer Identification Number (EIN), which is a 9-digit number with the format dd-ddddddd.

using System;
using System.Text.RegularExpressions;

public class Example
{
public static void Main()
{
string pattern = @"\b(\d{2}-\d{7}|\d{3}-\d{2}-\d{4})\b";
string input = "01-9999999 020-333333 777-88-9999";
Console.WriteLine("Matches for {0}:", pattern);
foreach (Match match in Regex.Matches(input, pattern))
Console.WriteLine("   {0} at position {1}", match.Value, match.Index);
}
}
// The example displays the following output:
//       Matches for \b(\d{2}-\d{7}|\d{3}-\d{2}-\d{4})\b:
//          01-9999999 at position 0
//          777-88-9999 at position 22

Imports System.Text.RegularExpressions

Module Example
Public Sub Main()
Dim pattern As String = "\b(\d{2}-\d{7}|\d{3}-\d{2}-\d{4})\b"
Dim input As String = "01-9999999 020-333333 777-88-9999"
Console.WriteLine("Matches for {0}:", pattern)
For Each match As Match In Regex.Matches(input, pattern)
Console.WriteLine("   {0} at position {1}", match.Value, match.Index)
Next
End Sub
End Module
' The example displays the following output:
'       Matches for \b(\d{2}-\d{7}|\d{3}-\d{2}-\d{4})\b:
'          01-9999999 at position 0
'          777-88-9999 at position 22


\b 從字緣開始。Start at a word boundary.
(\d{2}-\d{7}|\d{3}-\d{2}-\d{4}) 比對下列其中一項：兩個十進位數字後接連字號再後接七個十進位數字，或是三個十進位數字、連字號、兩個十進位數字、另一個連字號及四個十進位數字。Match either of the following: two decimal digits followed by a hyphen followed by seven decimal digits; or three decimal digits, a hyphen, two decimal digits, another hyphen, and four decimal digits.
\d 結束字緣比對。End the match at a word boundary.

## 使用運算式進行的條件式比對Conditional matching with an expression

(?( expression ) yes | no )(?( expression ) yes | no )

(?(?= expression ) yes | no )(?(?= expression ) yes | no )

using System;
using System.Text.RegularExpressions;

public class Example
{
public static void Main()
{
string pattern = @"\b(?(\d{2}-)\d{2}-\d{7}|\d{3}-\d{2}-\d{4})\b";
string input = "01-9999999 020-333333 777-88-9999";
Console.WriteLine("Matches for {0}:", pattern);
foreach (Match match in Regex.Matches(input, pattern))
Console.WriteLine("   {0} at position {1}", match.Value, match.Index);
}
}
// The example displays the following output:
//       Matches for \b(\d{2}-\d{7}|\d{3}-\d{2}-\d{4})\b:
//          01-9999999 at position 0
//          777-88-9999 at position 22

Imports System.Text.RegularExpressions

Module Example
Public Sub Main()
Dim pattern As String = "\b(?(\d{2}-)\d{2}-\d{7}|\d{3}-\d{2}-\d{4})\b"
Dim input As String = "01-9999999 020-333333 777-88-9999"
Console.WriteLine("Matches for {0}:", pattern)
For Each match As Match In Regex.Matches(input, pattern)
Console.WriteLine("   {0} at position {1}", match.Value, match.Index)
Next
End Sub
End Module
' The example displays the following output:
'       Matches for \b(?(\d{2}-)\d{2}-\d{7}|\d{3}-\d{2}-\d{4})\b:
'          01-9999999 at position 0
'          777-88-9999 at position 22


\b 從字緣開始。Start at a word boundary.
(?(\d{2}-) 判斷接下來三個字元是否為兩個數字後接連字號。Determine whether the next three characters consist of two digits followed by a hyphen.
\d{2}-\d{7} 如果上一個模式符合，便會比對兩個數字，後接連字號，再後接七個數字。If the previous pattern matches, match two digits followed by a hyphen followed by seven digits.
\d{3}-\d{2}-\d{4} 如果上一個模式不符合，便會比對三個十進位數字、連字號、兩個十進位數字、另一個連字號，以及四個十進位數字。If the previous pattern does not match, match three decimal digits, a hyphen, two decimal digits, another hyphen, and four decimal digits.
\b 比對字邊界。Match a word boundary.

## 依據有效擷取群組進行的條件式比對Conditional matching based on a valid captured group

(?( name ) yes | no )(?( name ) yes | no )

or

(?( 數字 ) | no )(?( number ) yes | no )

using System;
using System.Text.RegularExpressions;

public class Example
{
public static void Main()
{
string pattern = @"\b(?<n2>\d{2}-)?(?(n2)\d{7}|\d{3}-\d{2}-\d{4})\b";
string input = "01-9999999 020-333333 777-88-9999";
Console.WriteLine("Matches for {0}:", pattern);
foreach (Match match in Regex.Matches(input, pattern))
Console.WriteLine("   {0} at position {1}", match.Value, match.Index);
}
}
// The example displays the following output:
//       Matches for \b(?<n2>\d{2}-)?(?(n2)\d{7}|\d{3}-\d{2}-\d{4})\b:
//          01-9999999 at position 0
//          777-88-9999 at position 22

Imports System.Text.RegularExpressions

Module Example
Public Sub Main()
Dim pattern As String = "\b(?<n2>\d{2}-)?(?(n2)\d{7}|\d{3}-\d{2}-\d{4})\b"
Dim input As String = "01-9999999 020-333333 777-88-9999"
Console.WriteLine("Matches for {0}:", pattern)
For Each match As Match In Regex.Matches(input, pattern)
Console.WriteLine("   {0} at position {1}", match.Value, match.Index)
Next
End Sub
End Module


\b 從字緣開始。Start at a word boundary.
(?<n2>\d{2}-)? 比對出現零次或一次且後接連字號的兩個數字。Match zero or one occurrence of two digits followed by a hyphen. 將此擷取群組命名為 n2Name this capturing group n2.
(?(n2) 測試 n2 在輸入字串中是否相符。Test whether n2 was matched in the input string.
\d{7} 如果 n2 相符，則會比對七個十進位數字。If n2 was matched, match seven decimal digits.
|\d{3}-\d{2}-\d{4} 如果 n2 不相符，則會比對三個十進位數字、一個連字號、兩個十進位數字、另一個連字號，以及四個十進位數字。If n2 was not matched, match three decimal digits, a hyphen, two decimal digits, another hyphen, and four decimal digits.
\b 比對字邊界。Match a word boundary.

using System;
using System.Text.RegularExpressions;

public class Example
{
public static void Main()
{
string pattern = @"\b(\d{2}-)?(?(1)\d{7}|\d{3}-\d{2}-\d{4})\b";
string input = "01-9999999 020-333333 777-88-9999";
Console.WriteLine("Matches for {0}:", pattern);
foreach (Match match in Regex.Matches(input, pattern))
Console.WriteLine("   {0} at position {1}", match.Value, match.Index);
}
}
// The example display the following output:
//       Matches for \b(\d{2}-)?(?(1)\d{7}|\d{3}-\d{2}-\d{4})\b:
//          01-9999999 at position 0
//          777-88-9999 at position 22

Imports System.Text.RegularExpressions

Module Example
Public Sub Main()
Dim pattern As String = "\b(\d{2}-)?(?(1)\d{7}|\d{3}-\d{2}-\d{4})\b"
Dim input As String = "01-9999999 020-333333 777-88-9999"
Console.WriteLine("Matches for {0}:", pattern)
For Each match As Match In Regex.Matches(input, pattern)
Console.WriteLine("   {0} at position {1}", match.Value, match.Index)
Next
End Sub
End Module
' The example displays the following output:
'       Matches for \b(\d{2}-)?(?(1)\d{7}|\d{3}-\d{2}-\d{4})\b:
'          01-9999999 at position 0
'          777-88-9999 at position 22