本文整理汇总了C#中Regex.GroupNumberFromName方法的典型用法代码示例。如果您正苦于以下问题:C# Regex.GroupNumberFromName方法的具体用法?C# Regex.GroupNumberFromName怎么用?C# Regex.GroupNumberFromName使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Regex
的用法示例。
在下文中一共展示了Regex.GroupNumberFromName方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Parser
public Parser()
{
_Regex = new Regex(
@"((?'number'(-?)[0-9]+(\.[0-9]+)?)|(?'identifier'[_A-Za-z][A-Za-z0-9_]*)|(?'operator'<=|>=|!=|[\-+/*&|^!><=.%?:@])|(?'paren'[(){}]))|(?'comma',)",
RegexOptions.IgnorePatternWhitespace | RegexOptions.ExplicitCapture
);
_Group_Number = _Regex.GroupNumberFromName("number");
_Group_Identifier = _Regex.GroupNumberFromName("identifier");
_Group_Operator = _Regex.GroupNumberFromName("operator");
_Group_Paren = _Regex.GroupNumberFromName("paren");
_Group_Comma = _Regex.GroupNumberFromName("comma");
}
示例2: VerifyGroupNumbers
public static bool VerifyGroupNumbers(Regex r, String[] expectedNames, int[] expectedNumbers)
{
int[] numbers = r.GetGroupNumbers();
if (numbers.Length != expectedNumbers.Length)
{
Console.WriteLine("Err_7978awoyp! Expect {0} numbers actual={1}", expectedNumbers.Length, numbers.Length);
return false;
}
for (int i = 0; i < expectedNumbers.Length; i++)
{
if (numbers[i] != expectedNumbers[i])
{
Console.WriteLine("Err_4342asnmc! Expected GroupNumbers[{0}]={1} actual={2}", i, expectedNumbers[i], numbers[i]);
return false;
}
if (expectedNumbers[i] != r.GroupNumberFromName(expectedNames[i]))
{
Console.WriteLine("Err_98795ajkas!GroupNumberFromName({0})={1} actual={2}", expectedNames[i], expectedNumbers[i], r.GroupNumberFromName(expectedNames[i]));
return false;
}
}
return true;
}
示例3: Groups
public void Groups(string pattern, string input, RegexOptions options, CultureInfo cultureInfo, string[] expectedGroups)
{
CultureInfo originalCulture = CultureInfo.CurrentCulture;
try
{
// In invariant culture, the unicode char matches differ from expected values provided.
if (originalCulture.Equals(CultureInfo.InvariantCulture))
{
CultureInfo.CurrentCulture = s_enUSCulture;
}
if (cultureInfo != null)
{
CultureInfo.CurrentCulture = cultureInfo;
}
Regex regex = new Regex(pattern, options);
Match match = regex.Match(input);
Assert.True(match.Success);
Assert.Equal(expectedGroups.Length, match.Groups.Count);
Assert.True(expectedGroups[0] == match.Value, string.Format("Culture used: {0}", CultureInfo.CurrentCulture));
int[] groupNumbers = regex.GetGroupNumbers();
string[] groupNames = regex.GetGroupNames();
for (int i = 0; i < expectedGroups.Length; i++)
{
Assert.Equal(expectedGroups[i], match.Groups[groupNumbers[i]].Value);
Assert.Equal(match.Groups[groupNumbers[i]], match.Groups[groupNames[i]]);
Assert.Equal(groupNumbers[i], regex.GroupNumberFromName(groupNames[i]));
Assert.Equal(groupNames[i], regex.GroupNameFromNumber(groupNumbers[i]));
}
}
finally
{
if (cultureInfo != null || originalCulture.Equals(CultureInfo.InvariantCulture))
{
CultureInfo.CurrentCulture = originalCulture;
}
}
}
示例4: Run
//.........这里部分代码省略.........
}
}
catch (Exception e)
{
if (ExpectException && e.GetType() == _expectedExceptionType)
{
return true;
}
else if (ExpectException)
{
Console.WriteLine("Err_4980asu! Expected exception of type {0} and instead the following was thrown: \n{1}", _expectedExceptionType, e);
return false;
}
else
{
Console.WriteLine("Err_78394ayuua! Expected no exception to be thrown and the following was thrown: \n{0}", e);
return false;
}
}
m = r.Match(_input);
}
finally
{
if (null != _cultureInfo)
{
CultureInfo.CurrentCulture = originalCulture;
}
}
if (m.Success && !ExpectSuccess)
{
Console.WriteLine("Err_2270awanm! Did not expect the match to succeed");
return false;
}
else if (!m.Success && ExpectSuccess)
{
Console.WriteLine("Err_68997asnzxn! Did not expect the match to fail");
return false;
}
if (!ExpectSuccess)
{
//The match was not suppose to succeed and it failed. There is no more checking to do
//so the test was a success
return true;
}
if (m.Groups.Count != _expectedGroups.Length)
{
Console.WriteLine("Err_0234jah!Expected {0} groups and got {1} groups", _expectedGroups.Length, m.Groups.Count);
return false;
}
if (m.Value != _expectedGroups[0])
{
Console.WriteLine("Err_611074ahhar Expected Value='{0}' Actual='{1}'", _expectedGroups[0], m.Value);
return false;
}
int[] groupNumbers = r.GetGroupNumbers();
string[] groupNames = r.GetGroupNames();
for (int i = 0; i < _expectedGroups.Length; i++)
{
//Verify Group.Value
if (m.Groups[groupNumbers[i]].Value != _expectedGroups[i])
{
Console.WriteLine("Err_07823nhhl Expected Group[{0}]='{1}' actual='{2}' Name={3}",
groupNumbers[i], _expectedGroups[i], m.Groups[groupNumbers[i]], r.GroupNameFromNumber(groupNumbers[i]));
return false;
}
//Verify the same thing is returned from groups when using either an int or string index
if (m.Groups[groupNumbers[i]] != m.Groups[groupNames[i]])
{
Console.WriteLine("Err_08712saklj Expected Groups[{0}]='{1}' Groups[{2}]='{3}'",
groupNumbers[i], m.Groups[groupNumbers[i]], groupNames[i], m.Groups[groupNames[i]]);
return false;
}
//Verify GetGroupNumberFromName
if (groupNumbers[i] != r.GroupNumberFromName(groupNames[i]))
{
Console.WriteLine("Err_68974aehui Expected GroupNumberFromName({0})={1} actual{2}",
groupNames[i], groupNumbers[i], r.GroupNumberFromName(groupNames[i]));
return false;
}
//Verify GetGroupNameFromNumber
if (groupNames[i] != r.GroupNameFromNumber(groupNumbers[i]))
{
Console.WriteLine("Err_3468plhmy Expected GroupNameFromNumber({0})={1} actual{2}",
groupNumbers[i], groupNames[i], r.GroupNameFromNumber(groupNumbers[i]));
return false;
}
}
return true;
}
示例5: VerifyGroups
public static void VerifyGroups(Regex regex, string input, string[] expectedGroups, string[] expectedNames, int[] expectedNumbers)
{
Match match = regex.Match(input);
Assert.True(match.Success);
int[] numbers = regex.GetGroupNumbers();
Assert.Equal(expectedNumbers.Length, numbers.Length);
string[] names = regex.GetGroupNames();
Assert.Equal(expectedNames.Length, names.Length);
Assert.Equal(expectedGroups.Length, match.Groups.Count);
for (int i = 0; i < expectedNumbers.Length; i++)
{
Assert.Equal(expectedGroups[i], match.Groups[expectedNames[i]].Value);
Assert.Equal(expectedGroups[i], match.Groups[expectedNumbers[i]].Value);
Assert.Equal(expectedNumbers[i], numbers[i]);
Assert.Equal(expectedNumbers[i], regex.GroupNumberFromName(expectedNames[i]));
Assert.Equal(expectedNames[i], names[i]);
Assert.Equal(expectedNames[i], regex.GroupNameFromNumber(expectedNumbers[i]));
}
}
示例6: GroupNamesAndNumbers
public void GroupNamesAndNumbers(string pattern, string input, string[] expectedNames, int[] expectedNumbers, string[] expectedGroups)
{
Regex regex = new Regex(pattern);
Match match = regex.Match(input);
Assert.True(match.Success);
int[] numbers = regex.GetGroupNumbers();
Assert.Equal(expectedNumbers.Length, numbers.Length);
string[] names = regex.GetGroupNames();
Assert.Equal(expectedNames.Length, names.Length);
Assert.Equal(expectedGroups.Length, match.Groups.Count);
for (int i = 0; i < expectedNumbers.Length; i++)
{
Assert.Equal(expectedGroups[i], match.Groups[expectedNames[i]].Value);
Assert.Equal(expectedGroups[i], match.Groups[expectedNumbers[i]].Value);
Assert.Equal(expectedNumbers[i], numbers[i]);
Assert.Equal(expectedNumbers[i], regex.GroupNumberFromName(expectedNames[i]));
Assert.Equal(expectedNames[i], names[i]);
Assert.Equal(expectedNames[i], regex.GroupNameFromNumber(expectedNumbers[i]));
}
}
示例7: Groups
public void Groups(string pattern, string input, RegexOptions options, CultureInfo cultureInfo, string[] expectedGroups)
{
CultureInfo originalCulture = null;
try
{
if (cultureInfo != null)
{
originalCulture = CultureInfo.CurrentCulture;
CultureInfo.CurrentCulture = cultureInfo;
}
Regex regex = new Regex(pattern, options);
Match match = regex.Match(input);
Assert.True(match.Success);
Assert.Equal(expectedGroups.Length, match.Groups.Count);
Assert.Equal(expectedGroups[0], match.Value);
int[] groupNumbers = regex.GetGroupNumbers();
string[] groupNames = regex.GetGroupNames();
for (int i = 0; i < expectedGroups.Length; i++)
{
Assert.Equal(expectedGroups[i], match.Groups[groupNumbers[i]].Value);
Assert.Equal(match.Groups[groupNumbers[i]], match.Groups[groupNames[i]]);
Assert.Equal(groupNumbers[i], regex.GroupNumberFromName(groupNames[i]));
Assert.Equal(groupNames[i], regex.GroupNameFromNumber(groupNumbers[i]));
}
}
finally
{
if (cultureInfo != null)
{
CultureInfo.CurrentCulture = originalCulture;
}
}
}