c# - How to use regular expression with a string array -
i have string array
string [] ar = { "net" , "com" , "org"};
and want label writes "true" when user enter website example ends string in array
i tried this:
private void timer1_tick(object sender, eventargs e) { foreach ( string ex in ar) { if (regex.ismatch(txtbox.text, @"^(www\.)([\w]+)\.(" + ex + ")$")) { lbl.text = "true"; } else { lbl.text = "false"; } } }
when write example "www.google.com", label still writes false
. when write "www.google.org" label writes true
.
you using foreach loop completing ittrations, when ittration compare condition remove control loop e.g. org @ last of array index thatswhy found @ last of lopp ittrations, edit code as.
foreach ( string ex in ar) { if (regex.ismatch(txtbox.text, @"^(www\.)([\w]+)\.(" + ex + ")$")) { lbl.text = "true"; return; } else { lbl.text = "false"; } }
Comments
Post a Comment