如何截取这样的字段
我想在读取的字段数据中,截取第一个图片的路径/upshop/201109/24100019201.jpg,请问如何截取啊
<p>
龙年买恐龙</p>
<p style="text-align: center">
<img alt="恐龙" src="/upshop/201109/24100019201.jpg" style="width: 550px; height: 413px" /></p>
<p style="text-align: center">
<img alt="恐龙" src="/upshop/201109/24100019592.jpg" style="width: 600px; height: 450px" /></p>
<p style="text-align: center">
<img alt="恐龙" src="/upshop/201109/24100019543.jpg" style="width: 600px; height: 450px" /></p>
<p style="text-align: center">
<img alt="恐龙" src="/upshop/201109/24100019934.jpg" style="width: 550px; height: 413px" /></p>
<p style="text-align: center">
</p>
[解决办法]
string input = @"<p> 龙年买恐龙</p><p style=""text-align: center""> <img alt=""恐龙"" src=""/upshop/201109/24100019201.jpg"" style=""width: 550px; height: 413px"" /></p><p style=""text-align: center""> <img alt=""恐龙"" src=""/upshop/201109/24100019592.jpg"" style=""width: 600px; height: 450px"" /></p><p style=""text-align: center""> <img alt=""恐龙"" src=""/upshop/201109/24100019543.jpg"" style=""width: 600px; height: 450px"" /></p><p style=""text-align: center""> <img alt=""恐龙"" src=""/upshop/201109/24100019934.jpg"" style=""width: 550px; height: 413px"" /></p><p style=""text-align: center""> </p>"; Regex rgx = new Regex(@"(?is)(?<=src="")[^""]+?(?="")"); Console.WriteLine(rgx.Match(input));
[解决办法]
<p style="text-align: center">
<img alt="恐龙" src="/upshop/201109/24100019934.jpg" style="width: 550px; height: 413px" /></p>
<p style="text-align: center">
</p>
jquery来操作,更方便,记得将jquery.js引入即可
<script type="text/javascript">
$(function(){
var v = $("img:first").attr("src");
alert(v);
});
</script>