您的位置:首页 > 其它

.Net 获得字符串中开始和结束字符串中间得值

2013-12-18 18:13 211 查看
#region Copyright (C) 2009-2010 jasonxuvip , All rights reserved
3
4 /*********************Regular:文件信息说明**********************
5 *
6 * Copyright (C) 2009-2010 jasonxuvip , All rights reserved
7 *
8 * Guid1: f93e4e16-5534-4f34-aaf1-ac44134cca9c
9 * Guid2: f8e3ebd7-ff2e-434f-86a4-7b5b8d33d17f
10 * Guid3: 1ec3756e-3cbd-4819-a62c-8d8f720d261d
11 * Guid4: d4c80c6b-6737-4059-804f-418fb56c6651
12 * Guid5: 5fe7f369-cc97-45d0-a32c-26e836ff8761
13 * CLRVersion: 2.0.50727.3615
14 * 命名空间: OfficeReport.Other
15 * 文件名: Regular
16 * 新建项输入的名称: Regular
17 * 机器名称: JASONXU
18 * 注册组织名: 微软中国
19 * 创建年份: 2010年
20 *
21 * 功能描述: 1.
22 * 2.
23 *
24 * Created By JasonXu At 2010/12/30 13:01:50
25 *
26 *
27 *************************************************************************/
28 #endregion
29
30
31 using System;
32 using System.Collections.Generic;
33 using System.Linq;
34 using System.Text;
35 using System.Text.RegularExpressions;
36
37 namespace Jasonxu
38 {
39 public class Regular
40 {
41
42 /// <summary>
43 /// 获得字符串中开始和结束字符串中间得值
44 /// </summary>
45 /// <param name="str"></param>
46 /// <param name="beginStr">开始</param>
47 /// <param name="endStr">结束</param>
48 /// <returns></returns>
49 private static string GetStr(string str, string beginStr, string endStr)
50 {
51 Regex rg = new Regex("(?<=(" + beginStr + "))[.\\s\\S]*?(?=(" + endStr + "))", RegexOptions.Multiline | RegexOptions.Singleline);
52 return rg.Match(str).Value;
53 }
54 }
55 }
56

使用方法:

string oldStr = @"<b>我的博客地址</b>http://www.cnblogs.com/mypig";
string newStr =Regular.GetStr(oldStr, "<b>", "</b>");
//结果:我的博客地址
string newoldstr = oldStr.Replace(Regular.GetStr(oldStr, "<b>", "</b>"), "PigBlogs");
//结果:<b>PigBlogs</b>http://www.cnblogs.com/mypig
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: