I am trying to substrings if they have certain format. Substring Regex query is [CENAOD(xyx)]. I have done following code but when running this in cycle it says all results match which is wrong. Where I’ve done something wrong?
string strRegex = @"(\[CENAOD\((\S|\W)*\)\])*";
string strCenaOd = sReader["intro"].ToString()
if (Regex.IsMatch(strCenaOd, strRegex, RegexOptions.IgnoreCase))
{
string = (want to read content of ( ) = xyz in example)
}
Adding to @Kent’s and @leppie’s answers, the code surrounding the regex needs work, too. I think this is what you were trying for:
IsMatch()is a simple yes-or-no check, it doesn’t provide any way to retrieve the matched text.I especially want to comment on
(\S|\W)*, from your regex. First,\S|\Wis a very inefficient way to match any character..is usually all you need, but as Kent pointed out,[^)](i.e., any character except)) is more appropriate in this case. Also, by placing the*outside the round brackets, you’ll only ever capture the last character.([^)]*)captures all of them. For more details, read this.