求一个正则表达式,把"file:///D:/map/zs.gif"这样的字符串提取出来 图片格式支持,bmp,gif,jpg,jpeg 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 string content = @"file:///D:/map/zs.gif ";string strRegex = "file:///.+\\.(bmp|gif|jpg|jpeg)";Regex re = new Regex(strRegex, RegexOptions.IgnoreCase | RegexOptions.Multiline);string strValue = "";foreach (Match m in re.Matches(content)){ strValue += m.Groups[0].Value;} 谢谢楼上的,要把那样的字符串替换成这样的样式"../images/101aaa.gif"并且图片名称是我自己定义的.先谢谢了 string strValue = "";string strImg = "1010.gif";string content = @"file:///D:/map/zs.gif ";string strRegex = "file:///.+\\.(bmp|gif|jpg|jpeg)";strValue = Regex.Replace(content, strRegex, "../images/"+strImg , RegexOptions.IgnoreCase); 问题是这个strImg 是不一样的,里面包含多少图片就有不同名称的strImg ,有好方法吗? content = Regex.Replace(content, @"file:///.+\\.(bmp|gif|jpg|jpeg)", "images/img/aaa.gif", RegexOptions.IgnoreCase | RegexOptions.Multiline);我这样写为什么不会替换?把@号去掉,为什么只替换最前面的字符串,后面的都丢失? content = Regex.Replace(content, @"file:.*\.(bmp|gif|jpg|jpeg)", "images/img/aaa.gif", RegexOptions.IgnoreCase | RegexOptions.Multiline); string content = hidContent.Value; string strRegex = "file:///.+\\.(bmp|gif|jpg|jpeg)"; string strValue = ""; Regex r = new Regex(strRegex, RegexOptions.IgnoreCase); Match m = r.Match(content); while (m.Success) { for (int i =1; i <= m.Groups.Count; i++) { Group g = m.Groups[i]; strValue+="---匹配的组Group:" + i.ToString(); CaptureCollection cc = g.Captures; for (int j = 0; j < cc.Count; j++) { Capture c = cc[j]; strValue += "---找到的值Capture:" + j + "='" + c.Value + "', ---位置Position:=" + c.Index.ToString() + "---长度:" + c.Length.ToString() ; } } m = m.NextMatch(); }这样的代码 为什么得捕获到的是"jpg"??而且只能捕获前面的字符,后面捕获不到,正则表达式还有问题啊 for (int i =1; i <= m.Groups.Count; i++)这 i初始为0 Groups[0]是全匹配从1开始才是组 content = Regex.Replace(content, @"file:.*\.(bmp|gif|jpg|jpeg)", "images/img/aaa.gif", RegexOptions.IgnoreCase | RegexOptions.Multiline);你这个方法只能替换最前面一个,后面的都丢失了,你自己没试过吗老大? 终于知道你想表达什么了 加个?号file.*?\.(bmp|gif|jpg|jpeg)测试:file:///D:/map/zs.gifFHDDHfile:///D:/map/zs.bmp"这样的字符串提取出来(chx_xuxu)gifFHDDHfile:///dfg:/map/zs.jpg结果:*******************file:///D:/map/zs.gif*******************file:///D:/map/zs.bmp*******************file:///dfg:/map/zs.jpg 上面的?号有问题 用这个file.*?\.(bmp|gif|jpg|jpeg) OK 你开始举的例子太简单了 所以我的正则也就写简单了file://.*?/([^/]*?\.(bmp|gif|jpg|jpeg))这个可以 你要的在组1里 asp:Repeater中的CheckBox问题!!(急!!) 请大家帮我看一下这个问题 求 c# 判断一个IP地址是否为内网IP .net网页如何将数据传递给C#的程序处理? 关于一个文章程序和留言本的思路问题。 请问web part是什么 jscript不熟,帮个忙. 急求一个JAVASCRIPT 函数和.cs程序的数据交换问题???? treeview的发布问题没人会吗?已经三天了 请各位高高手帮我解决一下,如何用ASP来上传图片文件到数据库,并且在页面上显示出来! 高手帮忙,求一SQL语句. 求com991205源代码
string strRegex = "file:///.+\\.(bmp|gif|jpg|jpeg)";Regex re = new Regex(strRegex, RegexOptions.IgnoreCase | RegexOptions.Multiline);
string strValue = "";
foreach (Match m in re.Matches(content))
{
strValue += m.Groups[0].Value;
}
并且图片名称是我自己定义的.
先谢谢了
string strImg = "1010.gif";
string content = @"file:///D:/map/zs.gif ";
string strRegex = "file:///.+\\.(bmp|gif|jpg|jpeg)";
strValue = Regex.Replace(content, strRegex, "../images/"+strImg , RegexOptions.IgnoreCase);
string strRegex = "file:///.+\\.(bmp|gif|jpg|jpeg)";
string strValue = "";
Regex r = new Regex(strRegex, RegexOptions.IgnoreCase);
Match m = r.Match(content);
while (m.Success)
{
for (int i =1; i <= m.Groups.Count; i++)
{
Group g = m.Groups[i];
strValue+="---匹配的组Group:" + i.ToString();
CaptureCollection cc = g.Captures;
for (int j = 0; j < cc.Count; j++)
{
Capture c = cc[j];
strValue += "---找到的值Capture:" + j + "='" + c.Value + "', ---位置Position:=" + c.Index.ToString() + "---长度:" + c.Length.ToString() ;
}
}
m = m.NextMatch();
}这样的代码 为什么得捕获到的是"jpg"??而且只能捕获前面的字符,后面捕获不到,正则表达式还有问题啊
这 i初始为0
Groups[0]是全匹配
从1开始才是组
file.*?\.(bmp|gif|jpg|jpeg)
测试:file:///D:/map/zs.gifFHDDHfile:///D:/map/zs.bmp"这样的字符串提取出来(chx_xuxu)gifFHDDHfile:///dfg:/map/zs.jpg
结果:
*******************
file:///D:/map/zs.gif
*******************
file:///D:/map/zs.bmp
*******************
file:///dfg:/map/zs.jpg
file.*?\.(bmp|gif|jpg|jpeg)
file://.*?/([^/]*?\.(bmp|gif|jpg|jpeg))
这个可以 你要的在组1里