Regex.GroupNumberFromName(String) 方法

定義

傳回對應指定群組名稱的群組編號。Returns the group number that corresponds to the specified group name.

public:
 int GroupNumberFromName(System::String ^ name);
public int GroupNumberFromName (string name);
member this.GroupNumberFromName : string -> int
Public Function GroupNumberFromName (name As String) As Integer

參數

name
String

群組名稱,要轉換至對應群組編號的。The group name to convert to the corresponding group number.

傳回

對應指定群組名稱的群組編號;如果 name 不是有效的群組名稱,則為 -1。The group number that corresponds to the specified group name, or -1 if name is not a valid group name.

例外狀況

namenullname is null.

備註

正則運算式模式可以包含已命名或編號的「捕捉群組」,其中描述模式比對中的子運算式。A regular expression pattern may contain either named or numbered capturing groups, which delineate subexpressions within a pattern match. 編號群組是以語法(子運算式)分隔,並根據其在正則運算式中的順序指派數位。Numbered groups are delimited by the syntax (subexpression) and are assigned numbers based on their order in the regular expression. 命名群組是以語法(?<名稱>子運算式)或(?)分隔名稱'子運算式),其中name是用來識別子運算式的名稱。Named groups are delimited by the syntax (?<name>subexpression) or (?'name'subexpression), where name is the name by which the subexpression will be identified. (如需詳細資訊,請參閱群組結構)。GroupNumberFromName 方法會依其在正則運算式中的序數位置,識別命名群組和編號群組。(For more information, see Grouping Constructs.) The GroupNumberFromName method identifies both named groups and numbered groups by their ordinal positions in the regular expression. 序數位置零一律代表整個正則運算式。Ordinal position zero always represents the entire regular expression. 所有編號群組會接著在命名群組之前計算,而不論其在正則運算式模式中的實際位置為何。All numbered groups are then counted before named groups, regardless of their actual position in the regular expression pattern.

如果 name 是正則運算式模式中所存在之群組編號的字串標記法,則此方法會傳回該數位。If name is the string representation of a group number that is present in the regular expression pattern, the method returns that number. 如果 name 對應到正則運算式模式中的已命名的「捕捉群組」,則此方法會傳回其對應的數位。If name corresponds to a named capturing group that is present in the regular expression pattern, the method returns its corresponding number. name 與組名的比較會區分大小寫。The comparison of name with the group name is case-sensitive. 如果 name 未對應至捕捉群組的名稱或捕捉群組數目的字串表示,則此方法會傳回-1。If name does not correspond to the name of a capturing group or to the string representation of the number of a capturing group, the method returns -1.

適用於

另請參閱