c# 从正则表达式获取值

2024-12-31 03:53:10
推荐回答(3个)
回答1:

string oldstr = "baidu.+?wd=|baidu.+?word";
Regex rgx = new Regex(@".*?\?(?\w+)\=.*?\?(?\w+)", RegexOptions.Compiled);
string getstr1 = rgx.Match(oldstr).Result("${temp1}").ToString();
string getstr2 = rgx.Match(oldstr).Result("${temp2}").ToString();
TextBox2.Text = getstr1 + "\n" + getstr2;

回答2:

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

回答3:

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

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