正则表达式:如何在引号,逗号,空格和新行中抓取字符串?

时间:2013-08-06 01:02:23

标签: c# regex

我在.txt文件中有一个字符串列表,我想在引号,逗号,空格和新行中获取数据。

以下是列表示例:

  

CurCode“608”,“840”,“784”,“036”,“048”,“124”,“756”,“156”,“208”,“978”,“826”,“344 “,”360“,”376“,”356“,”392“,”410“,”414“,”484“,”458“,”578“,”554“,”634“,”643“, “682”,“752”,“702”,“764”,“901”,“840”,“704”,“710”

我尝试过对类似问题的评论采用不同的方法,但它们似乎对我不起作用。

4 个答案:

答案 0 :(得分:4)

var list = Regex.Matches(input, @"\d+").Cast<Match>().Select(m => m.Value)
                .ToList();

答案 1 :(得分:2)

您可以简单地拆分,修剪和删除引号:

var list =
    str.Split(new string[] {","}, StringSplitOptions.RemoveEmptyEntries)
    .Select(x => x.Trim().Replace("\"", ""));

答案 2 :(得分:0)

对于这种部分格式,正则表达式将满足您的需求:

//sample created with http://regexhero.net/tester/
string strRegex = @"""(?:\d+)""";
RegexOptions myRegexOptions = RegexOptions.None;
Regex myRegex = new Regex(strRegex, myRegexOptions);
string strTargetString = @"""608"", ""840"", ""784"", ""036"", ""048"", ""124"", ""756"", ""156"", ""208"", ""978"", ""826"", ""344"", ""360"", ""376"", ""356"", ""392"", ""410"", ""414"", ""484"", ""458"", ""578"", ""554"", ""634"", ""643"", ""682"", ""752"", ""702"", ""764"", ""901"", ""840"", ""704"", ""710""";

foreach (Match myMatch in myRegex.Matches(strTargetString))
{
  if (myMatch.Success)
  {
   //Do your stuff with the value here: myMatch.Groups[0].Value 
  }
}

答案 3 :(得分:0)

你也可以这样做:

使用字符串操作:(推荐)

示例代码:

var lst = sampleStr.Replace(""",""",",").Replace("CurCode ""","").TrimEnd('"').Split(',');

使用正则表达式尝试这种模式:

(?<=[,\s]\")(.*?)(?=\")

这种模式足以处理引用的数字以及一些字符串

Live Demo

示例代码:

MatchCollection mcol = regex.Matches(sampleStr,@"(?<=[,\s]\")(.*?)(?=\")");

foreach(Match m in mcoll)
{
    Debug.Print(m.ToString());  // See output window
}