Need help for c# Regex -
i'm trying create regex named groups matches of 4 following strings:
var club = "real madrid"; var city = "madrid"; var nation = "spain"; var string1 = club; var string2 = club + "," + city; var string3 = club + "(" + nation + ")"; var string4 = club + "," + city + "(" + nation + ")";
or in other words: string looks "club,city(nation)"
city , nation optional optional whitespaces included.
thanks help!
regex aelor added "(" sign after "^(?[^," match string "real madrid(spain)"
added code example too.
var strings = new list<string>() { "real madrid", "real madrid,madrid", "real madrid(spain)", "real madrid,madrid(spain)" }; foreach (var str in strings) { var match = regex.match(str, @"^(?<club>[^,]+)(?:,(?<city>[^\(]+))?(?:\((?<nation>[^\)]+)\))?$"); var name = match.groups["club"].value; var city = match.groups["city"].value; var country = match.groups["nation"].value; }
Comments
Post a Comment