Java正則表達式Regexp
語法
()() 分段匹配,两个group
可以通过group(0), group(1) 分别获取

替换所有的数字
formularStr = formularStr.replaceAll("\\d",(cell.getRowIndex()+1)+"");
字符串首尾添加abc
(^\\s*)|(\\s*$)
replaceAll("(^\\s*)|(\\s*$)", "abc")
替換{} 全部内容
String regStr = "{hundred} 123";
log.info(regStr.replaceAll("\\{.*?\\}","ok"));
提取英文公司名称
提取英文部分
[\\w\\d\\s\\-\\_\\.\\&\\\"\\,\\(\\)]+
//[\w\d\s.,]*
String companyName = "";
Matcher matcher = Pattern.compile("[\\w\\d\\s.,]*").matcher(_item.getCmCoName());
if(matcher.find()) {
companyName = matcher.group();
}
提取中文部分
Matcher matcher = Pattern.compile("[\\u4E00-\\u9FA5]+").matcher(_item.getCmCoName());
Guangdong Tomorrow Industry Co.,Ltd. 广东明天實業有限公司
本文介紹了Java中正則表達式的應用案例,包括替換數字、字符串首尾添加字符、替換特定格式內容及提取英文公司名稱等實用語法。對於從事Java開發的工作人員來說,這些技巧能有效提升開發效率。

3648

被折叠的 条评论
为什么被折叠?



