简体   繁体   English

用C#正则表达式替换字符串模式

[英]Replacing String Pattern with C# Regular Expression

I want to replace string matching specific pattern using C# regular expression. 我想用C#正则表达式替换字符串匹配特定模式。 I did try out various regular expression with Regex.Replace Function but none of them worked for me. 我确实用Regex.Replace函数尝试了各种正则表达式,但它们都没有为我工作。 Can anyone help me build correct regex to replace part of string. 谁能帮我构建正确的正则表达式来替换部分字符串。

Here is my input string. 这是我的输入字符串。 Regex should match string that starts with <message Severity="Error">Password will expire in 30 days then any characters (even new lines characters) till it finds closing </message> tag. 正则表达式应匹配以<message Severity="Error">Password will expire in 30 days开头的字符串<message Severity="Error">Password will expire in 30 days然后是任何字符(甚至换行符号),直到找到结束</message>标记。 If regex finds matching pattern then it should replace it with empty string. 如果正则表达式找到匹配的模式,那么它应该用空字符串替换它。

Input String : 输入字符串:

<message Severity="Error">Password will expire in 30 days.
Please update password using following instruction.
1. login to abc
2. change password.
</message>

You can use LINQ2XML but if you want regex 你可以使用LINQ2XML但如果你想要regex

<message Severity="Error">Password will expire in 30 days.*?</message>(?s)

OR 要么

In linq2Xml 在linq2Xml中

XElement doc=XElement.Load("yourXml.xml");

foreach(var elm in doc.Descendants("message"))
{
    if(elm.Attribute("Severity").Value=="Error")
        if(elm.Value.StartsWith("Password will expire in 30 days"))
        {
            elm.Remove();
        }
}
doc.Save("yourXml");\\don't forget to save :P

I know there are objections to the approach, but this works for me. 我知道这种方法存在异议,但这对我有用。 (I suspect you were probably missing the RegexOptions.SingleLine, which will allow the dot to match newlines.) (我怀疑你可能错过了RegexOptions.SingleLine,这将允许点匹配换行符。)

string input = "lorem ipsum dolor sit amet<message Severity=\"Error\">Password will    expire in 30 days.\nPlease update password using following instruction.\n"
        + "1. login to abc\n\n2. change password.\n</message>lorem ipsum dolor sit amet <message>another message</message>";

string pattern = @"<message Severity=""Error"">Password will expire in 30 days.*?</message>";

string result = Regex.Replace(input, pattern, "", RegexOptions.Singleline | RegexOptions.IgnoreCase);

//result = "lorem ipsum dolor sit ametlorem ipsum dolor sit amet <message>another message</message>"

Like said in comments - XML parsing may be better suited. 就像在评论中所说的那样 - XML解析可能更适合。 Also - this might not be the best solution, depending on what you're trying to achieve. 此外 - 这可能不是最佳解决方案,具体取决于您要实现的目标。 but here's passing unit test - you should be able to make sens of it. 但是这里通过单元测试 - 你应该能够理解它。

[TestMethod]
public void TestMethod1()
{
    string input = "<message Severity=\"Error\">Password will expire in 30 days.\n"
                    +"Please update password using following instruction.\n"
                    +"1. login to abc\n"
                    +"2. change password.\n"
                    +"</message>";
    input = "something other" + input + "something else";

    Regex r = new Regex("<message Severity=\"Error\">Password will expire in 30 days\\..*?</message>", RegexOptions.Singleline);
    input = r.Replace(input, string.Empty);

    Assert.AreEqual<string>("something othersomething else", input);
}

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

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