3

考虑下面的输入字符串。

var inputString = "Config: EbizNewTestProject.dll PaymentSOAUrl for Paid: http://111.11.11.111/Payment.asp?" + Environment.NewLine;
            inputString += "Exception: <Response> <ReturnCode>4000</ReturnCode> <SuccessCode>NO</SuccessCode> <ReturnDesc>System Error</ReturnDesc> <ReturnURL>http://localhost/Default.aspx</ReturnURL> <CustomParameter /> </Response>: " + Environment.NewLine;
            inputString += "Config: EbizNewTestProject.dll PaymentSOAUrl for Paid: http://111.11.11.111/Payment.asp? " + Environment.NewLine;
            inputString += "Exception: <Response> <ReturnCode>4000</ReturnCode> <SuccessCode>NO</SuccessCode> <ReturnDesc>System Error</ReturnDesc> <ReturnURL>http://localhost/Default.aspx</ReturnURL> <CustomParameter /> </Response>: " + Environment.NewLine;
            inputString += "Config: PartnerServicesTestBase.dll PaymentSOAUrl for Paid: https://172.31.26.38/Payment.asp? " + Environment.NewLine;
            inputString += "Exception: <Response> <ReturnCode>5000</ReturnCode> <SuccessCode>NO</SuccessCode> <ReturnDesc>System Error</ReturnDesc> <ReturnURL>http://localhost/Default.aspx</ReturnURL> <CustomParameter /> </Response>: " + Environment.NewLine;
            inputString += "Config: PartnerServicesTestBase.dll PaymentSOAUrl for Paid: https://172.31.26.38/Payment.asp? " + Environment.NewLine;
            inputString += "Exception: <Response> <ReturnCode>5000</ReturnCode> <SuccessCode>NO</SuccessCode> <ReturnDesc>System Error</ReturnDesc> <ReturnURL>http://localhost/Default.aspx</ReturnURL> <CustomParameter /> </Response>" + Environment.NewLine;
            inputString += "Config: EbizNewTestProject.dll PaymentSOAUrl for Paid: http://111.11.11.111/Payment.asp? " + Environment.NewLine;
            inputString += "Exception: <Response> <ReturnCode>4000</ReturnCode> <SuccessCode>NO</SuccessCode> <ReturnDesc>System Error</ReturnDesc> <ReturnURL>http://localhost/Default.aspx</ReturnURL> <CustomParameter /> </Response>: " + Environment.NewLine;

让我们观察“Exceptions”的字符串内容(给出的示例中共有 5 个)。

第一

Exception: <Response> <ReturnCode>4000</ReturnCode> <SuccessCode>NO</SuccessCode> <ReturnDesc>System Error</ReturnDesc> <ReturnURL>http://localhost/Default.aspx</ReturnURL> <CustomParameter /> </Response>: 

第二个

Exception: <Response> <ReturnCode>4000</ReturnCode> <SuccessCode>NO</SuccessCode> <ReturnDesc>System Error</ReturnDesc> <ReturnURL>http://localhost/Default.aspx</ReturnURL> <CustomParameter /> </Response>:

第三个

Exception: <Response> <ReturnCode>5000</ReturnCode> <SuccessCode>NO</SuccessCode> <ReturnDesc>System Error</ReturnDesc> <ReturnURL>http://localhost/Default.aspx</ReturnURL> <CustomParameter /> </Response>:

第四人

Exception: <Response> <ReturnCode>5000</ReturnCode> <SuccessCode>NO</SuccessCode> <ReturnDesc>System Error</ReturnDesc> <ReturnURL>http://localhost/Default.aspx</ReturnURL> <CustomParameter /> </Response>:

第五人

Exception: <Response> <ReturnCode>4000</ReturnCode> <SuccessCode>NO</SuccessCode> <ReturnDesc>System Error</ReturnDesc> <ReturnURL>http://localhost/Default.aspx</ReturnURL> <CustomParameter /> </Response>:

如果我们仔细看,第一种情况和第二种情况是相同的。第三和第四也是如此。第 5 个与第 1/2 个相同。

我需要做的是,如果第一个和第二个或连续的“异常”文本是相同的,那么留下第一个,其他的将被替换为“异常:--DO--”。如果找到一个相同的字符串但不是连续的,那么它将按原样显示。

此后,输出将是(原始字符串的所有内容都将保持不变,只有匹配的异常将被更新,如下所示)

第一

Exception: <Response> <ReturnCode>4000</ReturnCode> <SuccessCode>NO</SuccessCode> <ReturnDesc>System Error</ReturnDesc> <ReturnURL>http://localhost/Default.aspx</ReturnURL> <CustomParameter /> </Response>:

SecondOne(因为它与 Firstone 相同并且是连续的)

Exception: --DO--

ThirdOne(因为是新的)

Exception: <Response> <ReturnCode>5000</ReturnCode> <SuccessCode>NO</SuccessCode> <ReturnDesc>System Error</ReturnDesc> <ReturnURL>http://localhost/Default.aspx</ReturnURL> <CustomParameter /> </Response>:

FourthOne(因为它与 Thirdone 相同并且是连续的)

Exception: --DO--

FifthOne(虽然它与第一个/第二个相同,但不是连续的)

Exception: <Response> <ReturnCode>4000</ReturnCode> <SuccessCode>NO</SuccessCode> <ReturnDesc>System Error</ReturnDesc> <ReturnURL>http://localhost/Default.aspx</ReturnURL> <CustomParameter /> </Response>:

我做了这个程序。首先,我根据索引(开始和结束)获取异常的内容,然后将值存储在集合中。然后使用循环检查第一个和第二个的内容并进行所需的更新。

但我相信还有很多比这更好的解决方案(可能是正则表达式、linq、lambda组合。)

需要帮助以有效的方式解决此问题。

谢谢

4

3 回答 3

0

当谈到正则表达式时,我仍然是一个主要的新手,但似乎你可以使用它们来做到这一点......

我仍在努力解决这个问题,如果我不能足够快地到达那里,希望大师能指出你正确的方向,但这就是我目前所拥有的:

正则表达式匹配:

((Exception.*?)(?=Exception)){2}

基本上,查找以单词“Exception”开头的字符串并匹配所有内容,直到下一次看到单词“Exception”(但第二次不匹配),然后查找相同字符串的 2 个副本。

正则表达式替换:

$1Exception: --DO--

意思是,保留第一次出现并用“例外:--DO--”替换第二次

希望这至少能让你走上正确的道路,我也会继续玩下去。(再次,抱歉,Regex 对我来说还是相当新的,我只知道这将是一个很酷的解决方案)

于 2013-03-25T14:09:17.917 回答
-1

这是我的最终版本

var inputString = "Config: EbizNewTestProject.dll PaymentSOAUrl for Paid: http://111.11.11.111/Payment.asp?" + Environment.NewLine;
            inputString += "Exception: <Response> <ReturnCode>4000</ReturnCode> <SuccessCode>NO</SuccessCode> <ReturnDesc>System Error</ReturnDesc> <ReturnURL>http://localhost/Default.aspx</ReturnURL> <CustomParameter /> </Response>: " + Environment.NewLine;
            inputString += "Config: EbizNewTestProject.dll PaymentSOAUrl for Paid: http://111.11.11.111/Payment.asp? " + Environment.NewLine;
            inputString += "Exception: <Response> <ReturnCode>4000</ReturnCode> <SuccessCode>NO</SuccessCode> <ReturnDesc>System Error</ReturnDesc> <ReturnURL>http://localhost/Default.aspx</ReturnURL> <CustomParameter /> </Response>: " + Environment.NewLine;
            inputString += "Config: PartnerServicesTestBase.dll PaymentSOAUrl for Paid: https://172.31.26.38/Payment.asp? " + Environment.NewLine;
            inputString += "Exception: <Response> <ReturnCode>5000</ReturnCode> <SuccessCode>NO</SuccessCode> <ReturnDesc>System Error</ReturnDesc> <ReturnURL>http://localhost/Default.aspx</ReturnURL> <CustomParameter /> </Response>: " + Environment.NewLine;
            inputString += "Config: PartnerServicesTestBase.dll PaymentSOAUrl for Paid: https://172.31.26.38/Payment.asp? " + Environment.NewLine;
            inputString += "Exception: <Response> <ReturnCode>5000</ReturnCode> <SuccessCode>NO</SuccessCode> <ReturnDesc>System Error</ReturnDesc> <ReturnURL>http://localhost/Default.aspx</ReturnURL> <CustomParameter /> </Response>: " + Environment.NewLine;
            inputString += "Config: EbizNewTestProject.dll PaymentSOAUrl for Paid: http://111.11.11.111/Payment.asp? " + Environment.NewLine;
            inputString += "Exception: <Response> <ReturnCode>4000</ReturnCode> <SuccessCode>NO</SuccessCode> <ReturnDesc>System Error</ReturnDesc> <ReturnURL>http://localhost/Default.aspx</ReturnURL> <CustomParameter /> </Response>: " + Environment.NewLine;


            TextReader reader = new StringReader(inputString);
            string line = null;
            StringBuilder sb = new StringBuilder();
            string firstLine = null;


            //append the first line
            sb.AppendLine(reader.ReadLine());

            //read the first exception line
            firstLine = reader.ReadLine();
            sb.AppendLine(firstLine);



            while ((line = reader.ReadLine()) != null)
            {
                if (line.StartsWith("Exception:"))
                {

                    if (line == firstLine)
                    {
                        sb.AppendLine("Exception:-- Do --");
                    }

                    if (line != firstLine)
                    {
                        sb.AppendLine(line);
                        firstLine = line;

                    }
                }
                else
                {
                    sb.AppendLine(line);
                }
            }


            Console.WriteLine(sb.ToString());

            Console.ReadKey();
于 2013-03-27T02:08:45.890 回答
-1

我将介绍 XML 反序列化,它为您处理了很多痛苦:

进口:

using System.Xml.Serialization;
using System.Text.RegularExpressions;

代码:

//find the Reponses with regex
MatchCollection  matches = Regex.Matches(inputString, "<Response>(.)+</Response>");

XmlSerializer serializer = new XmlSerializer(typeof(Response));

List<Response> entityList = new List<Response>();


//Deserialize the reponses
foreach (Match item in matches)
{
    using(System.IO.TextReader rdr = new StringReader(item.Value))
    {
        Response entity = (Response)serializer.Deserialize(rdr);
        entity.Line = item.Value;
        entityList.Add(entity);
    }
}

//now you have real objects which you can treat however you want. Example just a loop or linq or whatever
for (int i = 0; i < entityList.Count; i++)
{
    if (i > 0 && entityList[i - 1].ReturnCode == entityList[i].ReturnCode)
        Console.Out.WriteLine("--DO--");
    else
        Console.Out.WriteLine(entityList[i].Line);
}

和实体:

public class Response
{
    [XmlElement("ReturnCode")]
    public int ReturnCode { get; set; }

    [XmlIgnore()]
    public string Line { get; set; }
}
于 2013-03-25T13:42:52.323 回答