[转帖]基于正则的全文查找替换_Android, Python及开发编程讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Android, Python及开发编程讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 3236 | 回复: 0   主题: [转帖]基于正则的全文查找替换        下一篇 
q563725885
注册用户
等级:少校
经验:1134
发帖:98
精华:1
注册:2013-5-7
状态:离线
发送短消息息给q563725885 加好友    发送短消息息给q563725885 发消息
发表于: IP:您无权察看 2013-5-8 15:26:22 | [全部帖] [楼主帖] 楼主

下面的代码会把原文中的人名加粗(加<b>标签)

Java代码
北京联动北方科技有限公司北京联动北方科技有限公司北京联动北方科技有限公司

  1. public static void main(String[] args) { 
  2.        Pattern p = Pattern.compile("(name)([=:])([a-zA-Z]+)", Pattern.CASE_INSENSITIVE); 
  3.        Matcher m = p 
  4.        .matcher("Money was transferred to from one person [name=John] to another [Name:Kent]"); 
  5.        StringBuffer newMsg = new StringBuffer(); 
  6.        while (m.find()) { 
  7.              m.appendReplacement(newMsg, "$1$2<b>$3</b>"); 
  8.        } 
  9.        m.appendTail(newMsg); 
  10.        System.out.println(newMsg.toString()); 



public static void main(String[] args) {
       Pattern p = Pattern.compile("(name)([=:])([a-zA-Z]+)", Pattern.CASE_INSENSITIVE);
       Matcher m = p
       .matcher("Money was transferred to from one person [name=John] to another [Name:Kent]");
       StringBuffer newMsg = new StringBuffer();
       while (m.find()) {
             m.appendReplacement(newMsg, "$1$2<b>$3</b>");
       }
       m.appendTail(newMsg);
       System.out.println(newMsg.toString());
}

;

执行结果:Money was transferred to from one person [name=<b>John</b>] to another [Name:<b>Kent</b>]




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论