String replaceAll 正则注意事项及特殊用法(xjl456852原创)

类似于正则的用法. \\\\四个反斜线,最后会表示为一个\反斜线.
String s = "a(bc)d_abcd";
System.out.println(s.replaceAll("_", "\\\\_"));
结果:
a(bc)d\_abcd
String testg = "amfooniceshow";
//$2 相当于对前面正则表达式的第二组进行引用
System.out.println(testg.replaceAll("(am)(foo)", "$2haha"));
结果:
foohahaniceshow


String test1 = "hahaamfooniceshowerqwhdfgsd";
System.out.println(test1.replaceAll("(?<sho>)wer", "${sho}123456"));
结果:
hahaamfoonicesho123456qwhdfgsd
//结果是1, 相当于有一个组,这种是()被认为是一个分组.
Pattern p = Pattern.compile("(?<xxx>)");
System.out.println(p.matcher("ab").groupCount());
//常规的结果是0, 相当于没有新建分组
Pattern pp = Pattern.compile("xxx");
System.out.println(pp.matcher("ab").groupCount());
//常规的非捕获组结果也是0, 相当于没有新建分组
Pattern ppp = Pattern.compile("(?<=xxx)");
System.out.println(ppp.matcher("ab").groupCount());
package com.xjl456852.manager;
import java.util.regex.Matcher;
/**
* Created by xjl456852 on 2017/3/9.
*/
public class StringTest {
public static void main(String args[]) {
String s = "a(bc)d_abcd";
System.out.println(s.replaceAll("_", "\\\\_"));
//会出错
// System.out.println(s.replaceAll("_", "\\\\$"));
System.out.println(s.replaceAll("_", "\\\\\\$"));
//可用Matcher.quoteReplacement() 对replaceAll的第二个参数进行转义
System.out.println(s.replaceAll("_", Matcher.quoteReplacement("\\$")));
System.out.println(s.replaceAll("_", "\\$"));
System.out.println(s.replaceAll("_", "\\."));
System.out.println(s.replaceAll("_", "."));
System.out.println(s.replaceAll("_", "\\\\%"));
System.out.println(s.replaceAll("_", "\\5"));
System.out.println(s.replaceAll("_", "5"));
System.out.println(s.replaceAll("_", "5"));
System.out.println(s.replaceAll("_", "\""));
System.out.println(s.replaceAll("_", "\\\""));
System.out.println(s.replaceAll("_", "\\${1}"));
//会出错 ${} 这个大括号里面不能是数字
// System.out.println(s.replaceAll("_", "${1}"));
String testg = "amfooniceshow";
//$2 相当于对前面正则表达式的第二组进行引用
System.out.println(testg.replaceAll("(am)(foo)", "$2haha"));
System.out.println("--------------");
String test = "hahaam${foo}niceshow";
System.out.println(test.replaceAll("\\$\\{.*\\}", "\\\\\\$\\{ss\\}"));
System.out.println(test.replaceAll("\\$\\{.*\\}", Matcher.quoteReplacement("\\${ss}")));
//会出现错误
// System.out.println(test1.replaceAll("\\$\\{.*\\}", "${ss}"));
}
}
a(bc)d\_abcd
a(bc)d\$abcd
a(bc)d\$abcd
a(bc)d$abcd
a(bc)d.abcd
a(bc)d.abcd
a(bc)d\%abcd
a(bc)d5abcd
a(bc)d5abcd
a(bc)d5abcd
a(bc)d"abcd
a(bc)d"abcd
a(bc)d${1}abcd
foohahaniceshow
--------------
hahaam\${ss}niceshow
hahaam\${ss}niceshow
package com.xjl456852.manager;
import java.util.regex.Pattern;
/**
* Created by xjl456852 on 2017/3/9.
*/
public class StringTest {
public static void main(String args[]) {
System.out.println("下面是${name}格式的replaceAll替换");
String test1 = "hahaamfooniceshowerqwhdfgsd";
System.out.println("--------------------");
//${name} 属于有名字的领宽度匹配的引用.name必须是大写或小写字母,不能是其它符号或者数字.这种用法真是少见,感觉没什么实际的意义.
//${name} 中的name,是一种分组的名字,也就是说在前面的正则中需要写入这样的名字.而且这个名字的写法比较特殊.
//我查看源码之后了解到这种写法必须是<?name>的格式.
//例如下面.它的意思是:sho为零宽度匹配,这个可以匹配wer
System.out.println(test1.replaceAll("(?<sho>)wer", "${sho}123456"));
//下面这两个虽然按理说不能匹配wer,因为<sh>中少了个o,但其实可以匹配wer(这种用法真是不知道在那种场景中才能用到),所以将wer替换为123456
System.out.println(test1.replaceAll("(?<sh>)wer", "123456"));
System.out.println(test1.replaceAll("(?<sh>)wer", "${sh}123456"));
//这个参数b中使用了$1,而(?<sh>)会被认为是一个分组,但是这个又是零宽度的,所以$1相当于没有引用.而$1已经被使用,所以1不会打印出来,只会打印23456
System.out.println(test1.replaceAll("(?<sh>)wer", "$123456"));
System.out.println("----------------------");
//下面这两个写的<xxx>这种字符是不存在的,但是后面的we也会被替换
System.out.println(test1.replaceAll("(?<xxx>)we", "123${xxx}456"));
//下面这个和上面那个一样,<xxx>这个是不存在的,但是ho和we都会被替换掉
System.out.println(test1.replaceAll("ho(?<xxx>)we", "123${xxx}456"));
//这个h后面少了o,后面紧接着是we,故无法匹配字符串,所以会以原字符串输出
System.out.println(test1.replaceAll("h(?<xxx>)we", "123${xxx}456"));
System.out.println("------------------");
//这个也匹配不到,所以无法替换,所以会以原字符串输出
System.out.println(test1.replaceAll("(?<sho>)dwer", "123456"));
//下面这两个都是领宽度匹配,前后都没条件,无论<>中写的是什么字符串,前后都没有条件,就不会匹配任何字符串,按领宽度匹配处理,所以会在每个字符串的前后都插入123.
System.out.println(test1.replaceAll("(?<sho>)", "123"));
System.out.println(test1.replaceAll("(?<xxx>)", "123"));
System.out.println("=============================");
//这个是用非捕获组进行的匹配.会在sh后面插入123
System.out.println(test1.replaceAll("(?<=sh)", "123"));
//其实用非捕获字也能实现这种每个字符的前后都插入相同目标字符串的功能.但是这个有一个特点就是原字符串中必须不包含xxx
System.out.println(test1.replaceAll("(?<!xxx)", "123"));
//比如下面的这个,原字符串中就包含sh,使用?<!sh ,意思是不是sh的字符串都插入123.这时结果跟上一个字符串相比就少了一组123.
// 因为检查到字符s时,发现不是sh,所以会插入123,再次检查到h时,发现是sh,所以不插入,这时会跳过.h后面就会少一组123
System.out.println(test1.replaceAll("(?<!sh)", "123"));
System.out.println("-------------");
//下面是一些其它的写法可以根据结果自己理解.
System.out.println(test1.replaceAll("(?<ha>)haam.*?(ce)(?<sh>).*?(fg)", "12${ha}34${sh}56"));
System.out.println(test1.replaceAll("(?<ha>)haam", "123456${ha}"));
System.out.println(test1.replaceAll("(ce)(?<sh>)", "1234${sh}56"));
System.out.println(test1.replaceAll("ce(?<sh>)", "123456"));
System.out.println("-------------");
System.out.println(test1.replaceAll("(?<sh>)ow", "1234${sh}56"));
System.out.println(test1.replaceAll("(?<sh>)ow", "123456"));
System.out.println("-------------");
System.out.println(test1.replaceAll("(ce)(?<sh>)(ow)", "1234${sh}56"));
System.out.println(test1.replaceAll("ce(?<sh>)ow", "1234${sh}56"));
System.out.println(test1.replaceAll("(?<sh>)", "1234${sh}56"));
System.out.println(test1.replaceAll("(?<haam>)", "123${haam}456"));
System.out.println("---------------------");
System.out.println(test1.replaceAll("(?<xxx>)", "123${xxx}456"));
System.out.println(test1.replaceAll("(?<xxx>)", "123456"));
//结果是1, 相当于有一个组,这种是()被认为是一个分组.
Pattern p = Pattern.compile("(?<xxx>)");
System.out.println(p.matcher("ab").groupCount());
//常规的结果是0, 相当于没有新建分组
Pattern pp = Pattern.compile("xxx");
System.out.println(pp.matcher("ab").groupCount());
//常规的非捕获组结果也是0, 相当于没有新建分组
Pattern ppp = Pattern.compile("(?<=xxx)");
System.out.println(ppp.matcher("ab").groupCount());
}
}
下面是${name}格式的replaceAll替换
--------------------
hahaamfoonicesho123456qwhdfgsd
hahaamfoonicesho123456qwhdfgsd
hahaamfoonicesho123456qwhdfgsd
hahaamfoonicesho23456qwhdfgsd
----------------------
hahaamfoonicesho123456rqwhdfgsd
hahaamfoonices123456rqwhdfgsd
hahaamfooniceshowerqwhdfgsd
------------------
hahaamfooniceshowerqwhdfgsd
123h123a123h123a123a123m123f123o123o123n123i123c123e123s123h123o123w123e123r123q123w123h123d123f123g123s123d123
123h123a123h123a123a123m123f123o123o123n123i123c123e123s123h123o123w123e123r123q123w123h123d123f123g123s123d123
=============================
hahaamfoonicesh123owerqwhdfgsd
123h123a123h123a123a123m123f123o123o123n123i123c123e123s123h123o123w123e123r123q123w123h123d123f123g123s123d123
123h123a123h123a123a123m123f123o123o123n123i123c123e123s123ho123w123e123r123q123w123h123d123f123g123s123d123
-------------
ha123456sd
ha123456fooniceshowerqwhdfgsd
hahaamfooni123456showerqwhdfgsd
hahaamfooni123456showerqwhdfgsd
-------------
hahaamfoonicesh123456erqwhdfgsd
hahaamfoonicesh123456erqwhdfgsd
-------------
hahaamfooniceshowerqwhdfgsd
hahaamfooniceshowerqwhdfgsd
123456h123456a123456h123456a123456a123456m123456f123456o123456o123456n123456i123456c123456e123456s123456h123456o123456w123456e123456r123456q123456w123456h123456d123456f123456g123456s123456d123456
123456h123456a123456h123456a123456a123456m123456f123456o123456o123456n123456i123456c123456e123456s123456h123456o123456w123456e123456r123456q123456w123456h123456d123456f123456g123456s123456d123456
---------------------
123456h123456a123456h123456a123456a123456m123456f123456o123456o123456n123456i123456c123456e123456s123456h123456o123456w123456e123456r123456q123456w123456h123456d123456f123456g123456s123456d123456
123456h123456a123456h123456a123456a123456m123456f123456o123456o123456n123456i123456c123456e123456s123456h123456o123456w123456e123456r123456q123456w123456h123456d123456f123456g123456s123456d123456
1
0
0
String replaceAll 正则注意事项及特殊用法(xjl456852原创)的更多相关文章
- JAVA中string.replace()和string.replaceAll()的区别及用法
乍一看,字面上理解好像replace只替换第一个出现的字符(受javascript的影响),replaceall替换所有的字符,其实大不然,只是替换的用途不一样. public String r ...
- [转]String.Replace 和 String.ReplaceAll 的区别
JAVA 中的 replace replaceAll 问题: 测试code System.out.println("1234567890abcdef -----> "+&qu ...
- Java String.replaceAll() 与后向引用(backreference)
问题 昨天看到一篇博文,文中谈到一道 Java 面试题: 给定一字符串,若该字符串中间包含 "*",则删除该 "*":若该字符串首字符或尾字符为 "* ...
- Java用代码演示String类中的以下方法的用法
用代码演示String类中的以下方法的用法 (1)boolean isEmpty(): 判断字符串是不是空串,如果是空的就返回true (2)char charAt(int index): 返回索引上 ...
- Java String.replaceAll()方法
声明 以下是java.lang.String.replaceAll()方法的声明 public String replaceAll(String regex, String replacement) ...
- String replaceAll(String regex,String str)满足正则表达式的部分替换为给定内容
package seday02;/*** * String replaceAll(String regex,String str)* @author xingsir*/public class Rep ...
- (转)正则表达式:string.replaceAll()中的特殊字符($ \)与matcher.appendReplacement
string.replaceAll中的特殊字符 string.replaceAll(String regex, String replacement)中的replacement参数即替换内容中含有特殊 ...
- 1.用代码演示String类中的以下方法的用法 (2018.08.09作业)
public class Test_001 { public static void main(String[] args) { String a = "德玛西亚!"; Strin ...
- String formate的语法解析及简单用法
转自:https://blog.csdn.net/jiangyu1013/article/details/52607257 package cn.wuxiangbin.StringFormat; im ...
随机推荐
- ECS服务器配置密钥登录及常用日志
一.介绍 1.SSH(22端口)是Secure Shell Protocol的简写,由IETF网络工作小组(Network Working Group)制定:在进行数据传输之前,SSH先对联机数据包通 ...
- jQuery:has()和jQuery:contains()及jQuery:empty
jQuery:has()和jQuery:contains()两个方法比较类似.不同点在于: has是判断标签的 contains是判断文本的 1.jQuery:has() <div>< ...
- 如何彻底卸载Vs2015
当我们卸载了VS2015想再安装VS软件的时候,发现安装路径根本更改不了. 网上查的由很多方法. 要真的找注册表去完全删除时非常繁琐的这里可以使用的方法就是 先下载卸载软件 https://githu ...
- P3207 [HNOI2010]物品调度
传送门 完了题目看错了--还以为所有的\(x,y\)都要一样--结果题解都没看懂-- 先考虑如果已经求出了所有的\(pos\)要怎么办,那么我们可以把\(0\)也看做是一个箱子,然后最后每个箱子都在一 ...
- Font Awesome矢量图标
下载 font-awesome 文件夹到您的项目中. 在HTML的 <head> 中引用font-awesome.min.css. 可以将Font Awesome图标使用在几乎任何地方,只 ...
- Js 使用小技巧总结(1)
1.Js 的时间控制,小于初始时间,大于截止时间 <script type="text/javascript"> window.onload = Game ...
- 浅谈算法——splay
BST(二叉查找树)是个有意思的东西,种类巨TM多,然后我们今天不讲其他的,我们今天就讲splay 首先,如果你不知道Splay是啥,你也得知道BST是啥 如上图就是一棵优美的BST,它对于每个点保证 ...
- select多选
1.css <style> .divBox{ width:400px; margin:100px auto; } .divBox span{ vertical-align:top; dis ...
- c# 线程浅析(代理 、Invoke、Lock)
前言:本来想根据自己的经验总结一下c#线程相关的知识点, 写之前看了一些其他人的博客,发现自己也就掌握了不到三分之一....希望通过这次的博客将自己的知识点补充一下,写出更直白的博客和初学者分享. 这 ...
- jQuery学习笔记(4)-设置元素的属性和样式
一.前言 本篇主要讲解如何使用jQuery获取和操作元素的属性和css样式 二."DOM属性"与元素属性 1.运行一下代码 <img src="/images/lo ...