110,534
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
try
{
string subjectString = "[111]jdfjf34[drr]4545[erer][9rfj0r]";
Regex regexObj = new Regex(@"(\[[^\]]*\])");
Match matchResults = regexObj.Match(subjectString);
while (matchResults.Success)
{
// matched text: matchResults.Value
// match start: matchResults.Index
// match length: matchResults.Length
Console.WriteLine(matchResults.Value);
matchResults = matchResults.NextMatch();
}
}
catch (ArgumentException ex)
{
// Syntax error in the regular expression
}
}
}
}
string str = "[111]jdfjf34[drr]4545[erer][9rfj0r]";
string pattern = @"\[(?<region>.*?)\]";
Regex regex = new Regex(pattern);
for (Match match = regex.Match(str); match.Success; match = match.NextMatch())
{
Console.WriteLine(string.Format("[{0}]", match.Groups["region"].Value));
}