• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

无信不立

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

【正则表达式】java应用正则表达式

一:简单应用

 /**
     *
     * '
     * &
     * '
     * &
     * &
     * '
     * '
     * '
     * sources=sdcg'hde&xyz'dfa&&ad'''
     * result=sdcghdexyzdfaad

     */
    public static void main(String[] args) {
        String regex="'|&";
        Pattern pattern=Pattern.compile(regex);
        String sources="sdcg'hde&xyz'dfa&&ad'''";
        Matcher matcher=pattern.matcher(sources);
        while(matcher.find()){
            System.out.println(matcher.group());
        }
        String result=matcher.replaceAll("");
        System.out.println("sources="+sources);
        System.out.println("result="+result);
    }

 

二:特例:“\”在java的正则表达式需要写成“\\\\”

 /**
     * \
     * &
     * \
     * &
     * &
     * sources=sdcg\hde&xyz\dfa&&ad'''
     * result=sdcghdexyzdfaad'''
     * 
     * @param args
     */
    public static void main(String[] args) {
        String regex="\\\\|&";
        Pattern pattern=Pattern.compile(regex);
        String sources="sdcg\\hde&xyz\\dfa&&ad'''";
        Matcher matcher=pattern.matcher(sources);
        while(matcher.find()){
            System.out.println(matcher.group());
        }
        String result=matcher.replaceAll("");
        System.out.println("sources="+sources);
        System.out.println("result="+result);
    }
View Code

 

posted on 2018-05-11 20:02  无信不立  阅读(326)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3