¦zyciis ¦ 求正则,提取/view_001_002_003.html中的001,002,003
admin 发表于 2010-04-24 | 来源:互联网 | 阅读:
提取/view_001_002_003.html中的001,002,003
1:要以"/view"开头
2:以".html"结束
取中中间的
“_”开头接其他不为换行的字符
如上
/view_001_002_003.html
结果要为
Group[0].Value=http://topic.csdn.net/u/20100422/14/"/view_001_002_003.html"
Group[1].Value=http://topic.csdn.net/u/20100422/14/"001"
Group[2].Value=http://topic.csdn.net/u/20100422/14/"002"
Group[3].Value=http://topic.csdn.net/u/20100422/14/"003"
谢谢
当个或固定个我的会写
如view_[.]*\.html
但不同的是我中间的"_XXXX"为示知个数
谢谢

请使用正则表达式进行匹配
进行两次进则吗先取出_001_002_003再进行一次正则吗?没有办法一次取出吗?
一次性使用正则不需要取出来
等等 谁个方法给你 你看看 稍微等下
upupup
先取出_001_002_003 在截取下吧
@"(?i)/view(?:_([^_.])*)*\.html"不是用 Groups而是用 Captures
C# code
正则 String pattern = @"/view_(/d{3})_(/d3)_(/d3).html$";
C# code
//接收要判断的字符串
String str = "/view_001_002_003.html";
//对字符串判断的正则表达示 –为数字定义变量 如cid 方式:?<cid> @是绝对字符 \n等字符会被判定为 "\n" ^表达示$ 绝对匹配
String pattern = @"/view_(?<cid>/d{3})_(?<cid2>/d3)_(?<cid3>/d3).html$";
//实例化一个Regex类
Regex re = new Regex(pattern);
//调用Match方法
Match mc = re.Match(str);
//进行判断 并接收值
if (mc.Success)
{
String cid = mc.Groups["cid"].Value;
String page = mc.Groups["page"].Value;
Label1.Text = "abc_" + cid + "_" + page + ".html";
}
后面的没改 这个是我资料里的 改了一段发出来 你看看 学习下
@"(?i)/view(?:_([^_.]*))*\.html"
C# code
System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(@"(?i)/view(?:_([^_.])*)*\.html");
var m = reg.Match("/view_001_002_003.html");
if (m.Success)
{
var c = m.Captures;
}
上面的怎么写啊。。一次就取出的
C# code
/view_(\d)+_(\d)+_(\d)+\.html
/view_(\d)+_(\d)+_(\d)+\.html———晕。我都说了,里面的参数是不固定数量的
C# code
using System;
using System.Text.RegularExpressions;
class Program
{
static void Main()
{
string s = "/view_001_002_003.html";
Match m = Regex.Match(s, @"(?i)/view(?:_([^_.]+))*\.html");
Console.WriteLine("[{0}]", m.Groups[0]);
foreach (Capture t in m.Groups[1].Captures)
{
Console.WriteLine("[{0}]", t);
}
}
}
/* 程序输出:
[/view_001_002_003.html]
[001]
[002]
[003]
*/
Match.Groups[i].ValueMatch.Groups[i].Captures[j].Value是字符串。
/view_(\d)+_(\d)+_(\d)+\.html———晕。我都说了,里面的参数是不固定数量的C# code
/view(_(\d)+)+\.html