正则表达式替换(C#)

时间:2019-06-12 11:16:20

标签: c# regex

如何制作正则表达式。替换以下文本:

1) "Name's",     "Sex", "Age", "Height_(in)", "Weight (lbs)"
2) " LatD", "LatM ", 'LatS', "NS", "LonD", "LonM", "LonS", "EW", "City", "State"

结果:

1) Name's, Sex, Age, Height (in), Weight (lbs)
2) LatD, LatM, LatS, NS, LonD, LonM, LonS, EW, City, State

括号之间的空格可以任何大小(示例1)。方括号中也可能有不正确的空格(示例2)。另外,可以使用“ _” 符号代替空格(示例1)。并且可以使用单引号代替双引号(示例2)。

结果是,单词必须用逗号和空格分隔。

我的代码段

StreamReader fileReader = new StreamReader(...);
var fileRow = fileReader.ReadLine();
fileRow = Regex.Replace(fileRow, "_", " ");
fileRow = Regex.Replace(fileRow, "\"", "");
var fileDataField = fileRow.Split(',');

2 个答案:

答案 0 :(得分:2)

我不太了解C#语法,但是此正则表达式可以完成这项工作:

  • 查找:(?:_|^["']\h*|\h*["']$|\h*["']\h*,\h*["']\h*)
  • 替换:A space

说明:

(?:                         # non capture group
    _                       # undersscore
  |                         # OR
    ^["']\h*                # beginning of line, quote or apostrophe, 0 or more horizontal spaces
  |                         # OR
    \h*["']$                # 0 or more horizontal spaces, quote or apostrophe, end of line
  |                         # OR
    \h*["']\h*              # 0 or more horizontal spaces, quote or apostrophe, 0 or more horizontal spaces
    ,                       #
    \h*["']\h*              # 0 or more horizontal spaces, quote or apostrophe, 0 or more horizontal spaces
)                           # end group

Demo

答案 1 :(得分:1)

一种简单的直接字符串操作方式如何?

using System;
using System.Linq;
static void Main(string[] args)
{
    string dirty1 = "\"Name's\",     \"Sex\", \"Age\", \"Height_(in)\", \"Weight (lbs)\"";
    string dirty2 = "\" LatD\", \"LatM \", 'LatS', \"NS\", \"LonD\", \"LonM\", \"LonS\", \"EW\", \"City\", \"State\"";
    Console.WriteLine(Clean(dirty1));
    Console.WriteLine(Clean(dirty2));

    Console.ReadKey();
}

private static string Clean(string dirty)
{
    return dirty.Split(',').Select(item => item.Trim(' ', '"', '\'')).Aggregate((a, b) => string.Join(", ", a, b));
}

private static string CleanNoLinQ(string dirty)
{
    string[] items = dirty.Split(',');
    for(int i = 0; i < items.Length; i++)
    {
        items[i] = items[i].Trim(' ', '"', '\'');
    }
    return String.Join(", ", items);
}

您甚至可以先用foreach替换成LinQ,然后用string.Join()替换。

易于理解-易于维护。