繁体   English   中英

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

[英]Regex: How to grab strings within quotes, commas, spaces, and new line?

我在.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”

我对类似问题的评论尝试了不同的方法,但是它们似乎对我没有用。

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

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

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

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

//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 
  }
}

您也可以这样操作:

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

样例代码:

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

使用正则表达式尝试以下模式:

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

这种模式足以处理带引号的数字和一些字符串

现场演示

样例代码:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM