正则表达式在字符串

时间:2015-09-18 13:43:05

标签: c#

我想删除过滤字后面出现的两个单词,并删除字符串中的过滤字。在下面一行:

MY FILE =“/ test / test.txt”VERSION = 3 CRC = 0x1ECC2C78 XYZ = ABC VERSION =“NOT APPLICABLE”

过滤器是VERSION,我想从上面的行中删除VERSION = 3和VERSION =“NOT APPLICABLE”。

所以上面这行应该是这样的:

MY FILE =“/ test / test.txt”CRC = 0x1ECC2C78 XYZ = ABC

像VERSION这样的过滤器参数将动态传递,线条也会有不同的格式。上面只是一个例子。

1 个答案:

答案 0 :(得分:2)

这个简单的正则表达式应该可以工作:

var s = "MY FILE = \"/test/test.txt\" VERSION = 3 CRC = 0x1ECC2C78 XYZ = ABC VERSION = \"NOT APPLICABLE\"";
var r = new Regex("(MY FILE.*) VERSION.*(CRC.*)VERSION.*");
var m = r.Match(s);
var output = string.Format("{0} {1}",m.Groups[1],m.Groups[2]);

输出

MY FILE = "/test/test.txt" CRC = 0x1ECC2C78 XYZ = ABC 

修改

在OP编辑问题之后,另一个解决方案。做出的假设:

  • 一行名称=值对
  • name可以有空格(即MY FILE),但不能用引号
  • 值可以包含空格,但必须使用引号(“MY VALUE”)

MY FILE = "/test/test.txt" VERSION = 3 CRC = 0x1ECC2C78 XYZ = ABC VERSION = "NOT APPLICABLE"

并更新了代码:

var original = "MY FILE = \"/test/test.txt\" VERSION = 3 CRC = 0x1ECC2C78 XYZ = ABC VERSION = \"NOT APPLICABLE\"";
var nameToRemove = "VERSION";
var stringBuilder = new StringBuilder();

var s = original;
while(s.Length>0)
{
    var getNameRegex = new Regex("(.*?)=");
    var name = getNameRegex.Match(s).Groups[1].ToString();
    s = s.Substring(name.Length+1);
    Regex getValueRegex;
    if (s.TrimStart().StartsWith("\""))
    {
        getValueRegex =new Regex("(\\s*\".*?\"\\s*)");
    }
    else
    {
        getValueRegex =new Regex("(\\s*.*?)\\s+");
    }
    var value = getValueRegex.Match(s).ToString();
    s = s.Substring(value.Length);

    if (name.Trim() != nameToRemove)
    {
        stringBuilder.Append(name).Append("=").Append(value);
    }   
}

Console.WriteLine ("original : {0}",original);
Console.WriteLine ("parsed   : {0}",stringBuilder.ToString());

并使用不同的单词调用上面的内容产生:

版本

original : MY FILE = "/test/test.txt" VERSION = 3 CRC = 0x1ECC2C78 XYZ = ABC VERSION = "NOT APPLICABLE"
parsed   : MY FILE = "/test/test.txt" CRC = 0x1ECC2C78 XYZ = ABC 

我的文件

original : MY FILE = "/test/test.txt" VERSION = 3 CRC = 0x1ECC2C78 XYZ = ABC VERSION = "NOT APPLICABLE"
parsed   : VERSION = 3 CRC = 0x1ECC2C78 XYZ = ABC VERSION = "NOT APPLICABLE"

<强> CRC

original : MY FILE = "/test/test.txt" VERSION = 3 CRC = 0x1ECC2C78 XYZ = ABC VERSION = "NOT APPLICABLE"
parsed   : MY FILE = "/test/test.txt" VERSION = 3 XYZ = ABC VERSION = "NOT APPLICABLE"

DUMMY (字符串中不存在)

original : MY FILE = "/test/test.txt" VERSION = 3 CRC = 0x1ECC2C78 XYZ = ABC VERSION = "NOT APPLICABLE"
parsed   : MY FILE = "/test/test.txt" VERSION = 3 CRC = 0x1ECC2C78 XYZ = ABC VERSION = "NOT APPLICABLE"