Regular Expression problem
-
I have a following regex in my program
01 private void myMethod 02 { 03 string strInput = "http://www.yahoo.com/"; 04 string p = strInput; 05 Regex myRegex = new Regex(@"http://(www\.)(?[^\.]+)\.com", RegexOptions.IgnoreCase); 06 07 MatchCollection MatchList = myRegex.Matches(strInput); 08 09 StringBuilder sb = new StringBuilder(); 10 sb.Append(MatchList.Count + " Matches found\n"); 11 sb.Append("==========================================\n"); 12 rtbOutput.Text = sb.ToString(); 13 }
But when i run the program i receie the following error An unhandled exception of type 'System.ArgumentException' occurred in System.dll Additional information: parsing "http://(www\.)(?[^\.]+)\.com" - Unrecognized grouping construct. What am I doing Wrong?o O º(`'·.,(`'·., ☆,.·''),.·'')º O o° »·'"`»* *☆ t4ure4n ☆* *«·'"`« °o O º(,.·''(,.·'' ☆`'·.,)`'·.,)º O o°
-
I have a following regex in my program
01 private void myMethod 02 { 03 string strInput = "http://www.yahoo.com/"; 04 string p = strInput; 05 Regex myRegex = new Regex(@"http://(www\.)(?[^\.]+)\.com", RegexOptions.IgnoreCase); 06 07 MatchCollection MatchList = myRegex.Matches(strInput); 08 09 StringBuilder sb = new StringBuilder(); 10 sb.Append(MatchList.Count + " Matches found\n"); 11 sb.Append("==========================================\n"); 12 rtbOutput.Text = sb.ToString(); 13 }
But when i run the program i receie the following error An unhandled exception of type 'System.ArgumentException' occurred in System.dll Additional information: parsing "http://(www\.)(?[^\.]+)\.com" - Unrecognized grouping construct. What am I doing Wrong?o O º(`'·.,(`'·., ☆,.·''),.·'')º O o° »·'"`»* *☆ t4ure4n ☆* *«·'"`« °o O º(,.·''(,.·'' ☆`'·.,)`'·.,)º O o°
If I'm not mistaken then (? marks the start of a named group (see named group in MSDN). Why do you have the ( and ) anyway?
-^-^-^-^-^- no risk no funk
-
I have a following regex in my program
01 private void myMethod 02 { 03 string strInput = "http://www.yahoo.com/"; 04 string p = strInput; 05 Regex myRegex = new Regex(@"http://(www\.)(?[^\.]+)\.com", RegexOptions.IgnoreCase); 06 07 MatchCollection MatchList = myRegex.Matches(strInput); 08 09 StringBuilder sb = new StringBuilder(); 10 sb.Append(MatchList.Count + " Matches found\n"); 11 sb.Append("==========================================\n"); 12 rtbOutput.Text = sb.ToString(); 13 }
But when i run the program i receie the following error An unhandled exception of type 'System.ArgumentException' occurred in System.dll Additional information: parsing "http://(www\.)(?[^\.]+)\.com" - Unrecognized grouping construct. What am I doing Wrong?o O º(`'·.,(`'·., ☆,.·''),.·'')º O o° »·'"`»* *☆ t4ure4n ☆* *«·'"`« °o O º(,.·''(,.·'' ☆`'·.,)`'·.,)º O o°
Take the '?' question mark out of the group and put it in front of it, actually marking the fact that (www\.) is optional.
SkyWalker