开发者

How to search for a pattern within a string?

I have a string which contains the following output

login;windows
db;sql
audit;failure

how do I check if this string contains the word "audit;failure"?

I have use the following code but was not successful:

 currLine = sr.ReadToEnd();
 string[] splited = Regex.Split(currLine, "~~~~~~~~~~~~~~");
 case1 = splited[0];
 string case1 = "";
 string pattern1 = "audit;failure";
 if (Regex.IsMatch(case1, pattern1)){
 console.writeline("success"!);
}

I must search through the variable case1 and not the string currLine tha开发者_高级运维ks in advance! :D


Also make sure that you remove the line string case1 = "";

if(case1.Contains("audit;failure"))
   console.writeline("success"!);


I think you just need to adjust case1 string ........

 string case1 = "";
 currLine = sr.ReadToEnd();
 string[] splited = Regex.Split(currLine, "~~~~~~~~~~~~~~");
 case1 = splited[0];
 string pattern1 = "audit;failure";
 if (Regex.IsMatch(case1, pattern1)){
 console.writeline("success"!);
}


Simply use String.Contains(), use a code like follows

if(sr.Contains("audit;failure")
{

}


One way to do it would be to split this first into the lines.

string[] lines = sr.split (new string[]{ Environment.LineBreak});
foreach(string line in lines){
    string[] parts = line.split(new char[] {';'});
    if(parts[0] == "audit")
        return parts[1];
}
return "not set";

This will return whatever stands behind "audit" and is easily adaptable to return whatever you need. The final return will be triggered if no audit-line was present.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜