c# 从正则表达式获取值

来源:百度知道 编辑:UC知道 时间:2024/06/07 07:20:57
==============================
baidu.+?wd=|baidu.+?word
==============================
以上是百度搜索的URL
正则表达式如上,如果符合上面的正则表达式,那么如何获取 wd 或者word

关键字的值呢?

http://www.baidu.com/s?wd=a

http://www.baidu.com/s?wd=(.+)
Match.Group[1].Value;

string oldstr = "baidu.+?wd=|baidu.+?word";
Regex rgx = new Regex(@".*?\?(?<temp1>\w+)\=.*?\?(?<temp2>\w+)", RegexOptions.Compiled);
string getstr1 = rgx.Match(oldstr).Result("${temp1}").ToString();
string getstr2 = rgx.Match(oldstr).Result("${temp2}").ToString();
TextBox2.Text = getstr1 + "\n" + getstr2;
希望对您有点帮助.

string oldstr = "baidu.+?wd=|baidu.+?word";
Regex rgx = new Regex(@".*?\?(?<temp1>\w+)\=.*?\?(?<temp2>\w+)", RegexOptions.Compiled);
string getstr1 = rgx.Match(oldstr).Result("${temp