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 ...
随机推荐
- COCI2012 TOY
有m种物品,n个箱子之中装着若干物品.问取出一些箱子后,所有m种物品都被选出的方案数. m<=20,n<=106 这道题很妙啊 深刻地利用了容斥 看到n=20,我们就想到了状压和容斥. 怎 ...
- jQuery 表格隔行变色插件
jQuery提供了用于扩展jQuery功能的方法,即jQuery.fn.extend()方法和jQuery.extend()方法. 基本的JS框架代码如下: ;(function($) { $.fn. ...
- laravel生命周期和核心思想
工欲善其事,必先利其器.在开发Xblog的过程中,稍微领悟了一点Laravel的思想.确实如此,这篇文章读完你可能并不能从无到有写出一个博客,但知道Laravel的核心概念之后,当你再次写起Larav ...
- SVN配置详解
原文:http://swjr.blog.com.cn/archives/2006/TheRoadToSubversion1authz.shtml http://www.dayuer.com/freeb ...
- Linux系统编程---文件I/O(open、read、write、lseek、close)
文件描述符 定义:对内核而言,文件描述符相当于一个文件的标识,它是一个非负整数,当打开(open)一个现有文件或者创建(creat)一个新文件时,内核会向进程返回一个文件描述符 在unix中(文件描述 ...
- 自动构造词法分析器的步骤——正规式转换为最小化DFA
正规式-->最小化DFA 1.先把正则式-->NFA(非确定有穷自动机) 涉及一系列分解规则 2.再把NFA通过"子集构造法"-->DFA 通过子集构造法将NFA ...
- SQL数据库语言基础
表的创建: 1.创建列(字段):列名+类型 2.设置主键列:能够唯一标识一条数据 3.设置唯一:内容不能重复 4.外键关系: 一张表(从表)其中的某列引用自另外一张表(主表)中的主键列 设计表: 数据 ...
- C#学习-多线程小练习
1.双色球案例 namespace _18双色球案例 { public partial class Form1 : Form { private bool IsRunning; private Lis ...
- swing jTable排序问题(点击表头排序)
1.JDK6自带排序实现: tableName.setAutoCreateRowSorter(true); 2.其实界面设计中勾选一个属性就搞定了: .
- codeforces_459D_(线段树,离散化,求逆序数)
链接:http://codeforces.com/problemset/problem/459/D D. Pashmak and Parmida's problem time limit per te ...