using System; using System.Text.RegularExpressions;class A { static void Main() { string s = "select * from table where datediff(year,birth,getdate())>=0 and datediff(year,birth,getdate())<=11"; foreach (Match m in Regex.Matches(s, @"\d+")) { Console.WriteLine(m.Value); } } } /* 程序输出: 0 11 */
不是这个意思 例如 select * from table where datediff(year,birth,getdate())>=0 and datediff(year,birth,getdate())<=11 and id>8要取出 datediff(year,birth,getdate())最小值 (就是取0) 还有最大值(就是11) 用正则怎么取
using System; using System.Text.RegularExpressions;class A { static void Main() { string s = "select * from table where datediff(year,birth,getdate())>=0 and datediff(year,birth,getdate())<=11 and id>8"; string p = @"(?i)datediff\(year,birth,getdate\(\)\)>=(?<min>\d+).*?datediff\(year,birth,getdate\(\)\)<=(?<max>\d+)"; Match m = Regex.Match(s, p); Console.WriteLine("min: {0} max: {1}", m.Groups["min"], m.Groups["max"]); } } /* 程序输出: min: 0 max: 11 */
using System.Text.RegularExpressions;class A
{
static void Main()
{
string s = "select * from table where datediff(year,birth,getdate())>=0 and datediff(year,birth,getdate())<=11";
foreach (Match m in Regex.Matches(s, @"\d+"))
{
Console.WriteLine(m.Value);
}
}
}
/* 程序输出:
0
11
*/
例如
select * from table where datediff(year,birth,getdate())>=0 and datediff(year,birth,getdate())<=11 and id>8要取出
datediff(year,birth,getdate())最小值 (就是取0) 还有最大值(就是11)
用正则怎么取
using System.Text.RegularExpressions;class A
{
static void Main()
{
string s = "select * from table where datediff(year,birth,getdate())>=0 and datediff(year,birth,getdate())<=11 and id>8";
string p = @"(?i)datediff\(year,birth,getdate\(\)\)>=(?<min>\d+).*?datediff\(year,birth,getdate\(\)\)<=(?<max>\d+)";
Match m = Regex.Match(s, p);
Console.WriteLine("min: {0} max: {1}", m.Groups["min"], m.Groups["max"]);
}
}
/* 程序输出:
min: 0 max: 11
*/