上一篇m.lom599介绍了正则表达式,本文m.lom599细数Java中的正则表达式应用,主要介绍在String中和Pattern与Matcher的用法
String中的正则表达式
matches
public boolean matches(String regex)
匹配某正则表达式:返回boolean类型值
例如:
//一段文字
String text =
"[2018-01-20] this is a text,,,and hello world,hello everyone,";
boolean bool = text.matches("^\\[20\\d\\d-\\d\\d-\\d\\d\\].*");
//true
replaceFirst
public String replaceFirst(String regex, String replacement)
替换第一个符合正则的值 替换为replacement
例如:替换第一个llo结尾的单词
//一段文字
String text =
"[2018-01-20] this is a text,,,and hello world,hello everyone,";
String result = text.replaceFirst("\\w+llo","world");
//[2018-01-20] this is a text,,,and world world,hello everyone,
replaceAll
public String replaceAll(String regex, String replacement)
替换第所有符合正则的值 替换为replacement
//一段文字
String text =
"[2018-01-20] this is a text,,,and hello world,hello everyone,";
result = text.replaceAll("\\w+llo", "world");
//[2018-01-20] this is a text,,,and world world,world everyone,
split
public String[] split(String regex)
根据给定正则表达式的匹配拆分此字符串,它是全部拆分,最后一个值如果是被拆分的字符串,则不计入,第一个如果是被拆分的字符串,则记为一个""
空字符串,中间的如果是几个被分隔的字符串,则记为""
空字符串
例如:按照,
拆分
String text =
",[2018-01-20] this is a text,,,and hello world,hello everyone,";
String[] arr = text.split(",", 2);
System.out.println(arr.length);
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
/**
6
[2018-01-20] this is a text
and hello world
hello everyone
**/
public String[] split(String regex, int limit)
根据匹配给定的正则表达式来拆分此字符串,limit 是限制拆分的次数,实际分解的次数是 limit-1 次,limit 就是分解后数组的 length。
String text = ",[2018-01-20] this is a text,,,and hello world,hello everyone,";
String[] arr = text.split(",", 2);
System.out.println(arr.length);
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
/**
2
[2018-01-20] this is a text,,,and hello world,hello everyone,
**/
注意replace方法
public String replace(CharSequence target, CharSequence replacement)
target的值是要替换的值,而不是正则表达式,所以与上面有区别.
所有target会被替换为replacement
例如:替换text中的hello
//一段文字
String text = "[2018-01-20] this is a text,,,and hello world,hello everyone,";
result = text.replace("hello", "world");
System.out.println("replace() "+ result);
//[2018-01-20] this is a text,,,and world world,world everyone,
Pattern 和 Matcher
Pattern
Matcher
Pattern 和 Matcher例子
需求将文件夹里面的数据转为json串样式: 形如:
文件夹里数据:
00望远镜(形)
结果数据:
{id:00, title:"00 望远镜", desc:"形", cardUrl:"https://www.iteway.com/uploads/super_memory/00.jpg"}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Map;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class FormatStr {
private static final Pattern PATTERN =
Pattern.compile("(\\d+)([\\u4e00-\\u9fa5]+)(()([\\u4e00-\\u9fa5]+)())");
public static void main(String[] args) throws IOException {
BufferedReader reader =
new BufferedReader(new FileReader(new File("/Users/zhaomingwei/Downloads/number")));
TreeMap<Integer,NumberEncode> treeMap = new TreeMap();
String line;
while ((line=reader.readLine()) !=null){
//System.out.println(line);
Matcher matcher = PATTERN.matcher(line);
while (matcher.find()){
treeMap.put(Integer.parseInt(matcher.group(1)),
new NumberEncode(matcher.group(1),
matcher.group(1)+" "+matcher.group(2),
matcher.group(4),
"https://www.iteway.com/uploads/super_memory/"+
matcher.group(1)+".jpg"
));
}
}
for (Map.Entry<Integer,NumberEncode> entry: treeMap.entrySet()){
System.out.print(entry.getValue());
System.out.println(",");
}
}
}
class NumberEncode{
private String id;
private String title;
private String desc;
private String cardUrl;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getCardUrl() {
return cardUrl;
}
public void setCardUrl(String cardUrl) {
this.cardUrl = cardUrl;
}
public NumberEncode(String id, String title, String desc, String cardUrl) {
this.id = id;
this.title = title;
this.desc = desc;
this.cardUrl = cardUrl;
}
@Override
public String toString() {
return "{" +
"id:" + id +
", title:\"" + title + '\"' +
", desc:\"" + desc + '\"' +
", cardUrl:\"" + cardUrl + '\"' +
'}';
}
}
01冷饮(音) 11筷子(形) 21鳄鱼(音) 31鲨鱼(音) 41蜥蜴(音) 51劳动者(意) 02铃儿(音) 12婴儿(音) 22鸳鸯(形) 32扇儿(音) 42柿儿(音) 52鼓儿(音) 03三脚(形) 13医生(音) 23耳塞(音) 33闪闪的钻石(音) 43石山(音) 53火山(音) 04零食(音) 14钥匙(音) 24闹钟(24小时) 34三丝巾(音) 44蛇(嘶嘶声) 54武士(音) 05礼物(蛋糕) 15鹦鹉(音) 25二胡(音) 35山虎(音) 45师傅(音) 55火车(呜呜声) 06手枪(形) 16石榴(音) 26二流子(音) 36山鹿(音) 46饲料(音) 56蜗牛(音) 07令旗(音) 17仪器(音) 27耳机(音) 37山鸡(音) 47司机(音) 57武器(音) 08溜冰鞋(形) 18腰包(音) 28恶霸(音) 38妇女(意) 48石板(音) 58尾巴(音) 09猫(意) 19药酒(音) 29二舅(音) 39三九胃态(音) 49湿狗(音) 59五角星(音) 10棒球(形) 20鹅蛋(形) 30三轮车(音) 40司令(音) 50武林(音) 60榴莲(音) 61儿童(节日) 71机翼(音) 81白蚁(音) 91球衣(音) 0呼啦圈(形) 62牛儿(音) 72企鹅(音) 82靶儿(音) 92球儿(音) 1树(形) 63流沙(音) 73花旗参(音) 83爬山(音) 93旧伞(音) 2鹅(形) 64律师(音) 74骑士(音) 84巴士(音) 94旧(首)饰 3耳朵(形) 65尿壶(音) 75西服(音) 85宝物(音) 95酒壶(音) 4红旗(形) 66蝌蚪(形) 76汽油(音) 86八路(音) 96旧炉(音) 5钩子(形) 67油漆(音) 77机器人(音) 87白旗(音) 97旧旗(音) 6勺子(形) 68喇叭(音) 78青蛙(音) 88爸爸(音) 98酒吧(音) 7锄头(形) 69料酒(音) 79汽球(音) 89芭蕉(音) 99舅舅(音) 8葫芦(形) 70冰淇凌(音) 80巴黎:铁塔(音) 90酒瓶(音) 00望远镜(形) 9口哨(形)