本文整理汇总了C#中Regex.GetGroupNumbers方法的典型用法代码示例。如果您正苦于以下问题:C# Regex.GetGroupNumbers方法的具体用法?C# Regex.GetGroupNumbers怎么用?C# Regex.GetGroupNumbers使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Regex
的用法示例。
在下文中一共展示了Regex.GetGroupNumbers方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetRegexUrlValues
private static void GetRegexUrlValues(string strRelativeUrl, ref string strReturnUrl, ref string strExpression, List<string> lstExpressions, Dictionary<string, string> dicValues, ref Regex rex)
{
try
{
int iGroupCount = rex.GetGroupNumbers().Length;
string strReplace = string.Empty;
for (int i = 1; i < iGroupCount; i++)
{
strReplace += "{$" + i + "}";
}
strReturnUrl = rex.Replace(strRelativeUrl, strReplace);
rex = new Regex("{(.*?)}", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
int counter = 0;
foreach (Match item in rex.Matches(strReturnUrl))
dicValues.Add(lstExpressions[counter++], item.Value.Substring(1, item.Value.Length - 2));
if (dicValues.Count == 0)
{
strExpression = Blogsa.Settings["permaexpression"].ToString();
string strExt = Blogsa.UrlExtension;
Regex rx = new Regex("{id}", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
Regex regexCode = new Regex(@"/(\d+)" + strExt + "$", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
Match match = regexCode.Match(strRelativeUrl);
if (match.Success && rx.Match(strExpression).Success)
{
string strVal = match.Value.Replace("/", "");
strVal = new Regex("." + Blogsa.UrlExtension + "$", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase).Replace(strVal, "");
dicValues.Add("{id}", strVal);
}
else
{
rx = new Regex("{name}", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
if (rx.Match(strExpression).Success)
{
regexCode = new Regex(@"/([A-Z0-9a-z-]+)" + strExt + "$", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
match = regexCode.Match(strRelativeUrl);
if (match.Success)
{
string strVal = match.Value.Replace("/", "");
strVal = new Regex(strExt + "$", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase).Replace(strVal, "");
dicValues.Add("{name}", strVal);
}
}
}
}
}
catch
{
}
}
示例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
public bool Run()
{
Regex r;
Match m;
System.Globalization.CultureInfo originalCulture = null;
try
{
if (null != _cultureInfo)
{
originalCulture = CultureInfo.CurrentCulture;
CultureInfo.CurrentCulture = _cultureInfo;
}
try
{
r = new Regex(_pattern, _options);
if (ExpectException)
{
Console.WriteLine("Err_09872anba! Expected Regex to throw {0} exception and none was thrown", _expectedExceptionType);
return false;
}
}
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}'",
//.........这里部分代码省略.........
示例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;
}
}
}