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

#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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值