nyghtwollf nyghtwollf - 1 year ago 78
C# Question

C# regex pattern don't work in own code but do in avalaible webtool for testing purpose

i guys heres is a piece of code i wrote.
i tested the regex pattern on different webtool, they work in them!
but in my C# code it doesn't, can you point me why?
goal to achieve
(IsMatch supposed to return true on every testing tool but not in this code it return false)

enter code here

//not original code splitted is variable so i give it a static value for the question purpose
var splitted = "\n/87238/he.james.lkjxzckj.jack.12357.asdasas.1231bfsg.wrew";

//original code
string final = "james-jack";

string finalRegex = "";

string[] regexSplit = final.Split(Convert.ToChar("-"));

finalRegex = String.Format(@"^(?=.*{0}).*$", String.Join(")(?=.*",regexSplit).ToLower());

Regex r = new Regex(finalRegex.ToLower(),RegexOptions.IgnoreCase);

if (r.IsMatch(splitted.ToLower()))
//do something.....

Answer Source

As @Wiktor Stribi┼╝ew said, if you use RegexOptions.Singleline it will work. You have a new line in splitted variable, so your regex need to match multiple lines or you need to remove the new line \n

Regex r = new Regex(finalRegex.ToLower(), RegexOptions.IgnoreCase | RegexOptions.Singleline);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download