简体   繁体   English

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

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

I have a list of strings in a .txt file, and I want to grab the data within quotes, commas, spaces, and new line. 我在.txt文件中有一个字符串列表,我想获取引号,逗号,空格和换行符中的数据。

Here is the example of the listings: 这是清单的示例:

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" 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”

I have tried the different approaches from comments on similar questions but they don't seem to work for me. 我对类似问题的评论尝试了不同的方法,但是它们似乎对我没有用。

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

You can simply split, trim and remove the quotes: 您可以简单地拆分,修剪和删除引号:

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

for this partícular format, regex will fill your needs: 对于这种特殊格式,正则表达式将满足您的需求:

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

You can also do it this way: 您也可以这样操作:

Using string manipulation: (Recommended) 使用字符串操作:(推荐)

Sample Code: 样例代码:

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

Using regex try out this pattern: 使用正则表达式尝试以下模式:

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

This pattern is enough capable to handle the quoted numbers and well as some string 这种模式足以处理带引号的数字和一些字符串

Live Demo 现场演示

Sample Code: 样例代码:

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