我需要拆分一个字符串,使用Regex提取字符串数组中的括号和数据,并保留括号。
提取自
1-2-3(0)(1)至
(0)
(1)我建造了这个Regex,但不能让它工作。
String phrase= "123(0)(1)"
String[] results = Regex.Split(phrase,"\\r+(?:\\(.*\\))");发布于 2019-05-17 13:33:25
您可以使用Regex.Matches方法代替
        string phrase = "123(0)(1)";
        string[] results = Regex.Matches(phrase, @"\(.*?\)").Cast<Match>().Select(m => m.Value).ToArray();发布于 2019-05-17 13:25:05
如果括号中的这两个方法总是在一起,您可以尝试使用子字符串方法。
phrase = phrase.Substring(phrase.FirstIndexOf("("));可能得在后面放-1。
发布于 2019-05-17 13:31:19
可以使用(\(\d\))模式提取括号中的数字。
https://regex101.com/r/chjyLN/1
例如。
var input = "1-2-3(0)(1)";
Regex pattern = new Regex(@"(\(\d\))");
var matches = pattern.Matches(input);
foreach (Match match in matches)
{
  foreach (Capture capture in match.Captures)
  {
      Console.WriteLine(capture.Value);
  }
}https://stackoverflow.com/questions/56187330
复制相似问题