java Matcher匹配头尾截取替换字符串
2017-10-13 13:46
441 查看
在java 编程中,我们常常有这样的需求:需要将一段字符串内的特定字符串,按照一定规则查找出来或替换,比如匹配文本开头规则和结束规则。
以下就是Matcher的使用:
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @Author changle
* @Time 17/10/12.
* @Desc 匹配头尾截取替换字符串
* 待替换内容:
* url: jdbc:mysql://${cot.identity.db.ip}:3306/${cot.identity.db.dbname}?useUnicode=true&characterEncoding=UTF8
* 替换后:
* url: jdbc:mysql://{{cot.identity.db.ip}}:3306/{{cot.identity.db.dbname}}?useUnicode=true&characterEncoding=UTF8
*/
public class CommonTest {
public static void main(String[] args) {
String str = "url: jdbc:mysql://${cot.identity.db.ip}:3306/${cot.identity.db.dbname}?useUnicode=true&characterEncoding=UTF8";
if (str.contains("${cot.")) {
Pattern leftpattern = Pattern.compile("\\$\\{");
Matcher leftmatcher = leftpattern.matcher(str);
Pattern rightpattern = Pattern.compile("}");
Matcher rightmatcher = rightpattern.matcher(str);
int begin = 0;
List<String> foundKeys = new ArrayList<>();
while (leftmatcher.find(begin)) {
rightmatcher.find(leftmatcher.start());
String configKey = str.substring(leftmatcher.start(), rightmatcher.end());
System.out.println(configKey.replace("${", "{{"));
foundKeys.add(configKey);
begin = rightmatcher.end();
}
System.out.println("原内容:"+str);
for (String foundkey : foundKeys){
str = str.replace(foundkey, foundkey.replace("${cot.","{{cot.").replace("}", "}}").replace("-","_"));
}
System.out.println("替换后:"+str);
}
}
}
以下就是Matcher的使用:
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @Author changle
* @Time 17/10/12.
* @Desc 匹配头尾截取替换字符串
* 待替换内容:
* url: jdbc:mysql://${cot.identity.db.ip}:3306/${cot.identity.db.dbname}?useUnicode=true&characterEncoding=UTF8
* 替换后:
* url: jdbc:mysql://{{cot.identity.db.ip}}:3306/{{cot.identity.db.dbname}}?useUnicode=true&characterEncoding=UTF8
*/
public class CommonTest {
public static void main(String[] args) {
String str = "url: jdbc:mysql://${cot.identity.db.ip}:3306/${cot.identity.db.dbname}?useUnicode=true&characterEncoding=UTF8";
if (str.contains("${cot.")) {
Pattern leftpattern = Pattern.compile("\\$\\{");
Matcher leftmatcher = leftpattern.matcher(str);
Pattern rightpattern = Pattern.compile("}");
Matcher rightmatcher = rightpattern.matcher(str);
int begin = 0;
List<String> foundKeys = new ArrayList<>();
while (leftmatcher.find(begin)) {
rightmatcher.find(leftmatcher.start());
String configKey = str.substring(leftmatcher.start(), rightmatcher.end());
System.out.println(configKey.replace("${", "{{"));
foundKeys.add(configKey);
begin = rightmatcher.end();
}
System.out.println("原内容:"+str);
for (String foundkey : foundKeys){
str = str.replace(foundkey, foundkey.replace("${cot.","{{cot.").replace("}", "}}").replace("-","_"));
}
System.out.println("替换后:"+str);
}
}
}
相关文章推荐
- JAVA insert() 插入字符串 reverse() 颠倒 delete()和deleteCharAt() 删除字符 replace() 替换 substring() 截取子串
- perl中操作mysql,字符匹配与替换,字符串截取,计算字符长度
- Java正则表达式,匹配、替换字符串中的占位符
- java字符串操作:匹配、替换、萃取、分割拆分
- Java中repalceAll只替换第二个匹配到的字符串
- iOS NSString 字符串处理:截取字符串、匹配字符串、分隔字符串、拼接字符串、替换字符串
- java中的正则表达式 字符串的拆分,匹配,替换
- java 字符串截取和替换
- 黑马程序员_Java第25天知识总结_正则表达式_匹配_切割_替换_获取_Pattern_Matcher
- Java中String字符串的截取、替换、删除等操作
- java基础字符串截取,替换
- java 正则匹配空格字符串 正则表达式截取字符串
- java Pattern和Matcher——用于匹配和查找字符串中的内容
- java字符串截取、替换、删除、添加、查询等常用方法
- java正则匹配替换字符串中ip格式的字符串
- 正则表达式-字符串基本的匹配,拆分,替换和截取
- JAVA从字符串中截取部分匹配的字符串
- 关于java 截取特定字符串的探索
- python 正则表达式 字符串的 匹配 替换 分割 查找
- java中正则表达式截取字符串