1、转移字符
B 指定字符B
\xhh 十六进制值为0xhh的字符
\uhhhh 十六进制表示为0xhhhh的Unicode字符
\t 制表符
\n 换行符
\r 回车
\f 换页
\e 转移(Escape)
2、字符类
. 任意字符
[abc] 包含a、b、c任何一个字符
[^abc] 除了a、b、c之外的任何字符
\s 空白字符(空格、tab、换行、换页和回车)
\S 非空白字符[^\s]
\d 数字[0-9]
\D 非数字[^0-9]
\w 词字符[a-zA-Z0-9]
\W 非词字符
3、逻辑操作符
XY Y跟在X后面
| 或
(X) 捕获组。可以在表达式中用\i引用第i个捕获组。组号为0表示整个表达式。
4、边界匹配符
^ 一行的开始
$ 一行的结束
\b 词的边界
\B 非词的边界
\G 前一个匹配的结束
5、量词
? 零个或一个
* 零个或多个
+ 一个或多个
X{n} 恰好n次X
X{n,} 至少n次X
X{n,m} X至少n次,且不超过m次
1./**
2. * 用下划线分隔每个单词
3. */
4.private static String insertUnderline(String text){
5. String regEx = "([A-Z])";
6. Pattern p = Pattern.compile(regEx);
7. Matcher m = p.matcher(text);
8. StringBuffer sb = new StringBuffer("");
9. while(m.find()){
10. m.appendReplacement(sb,"_" + m.group());
11. }
12. m.appendTail(sb);
13. String tmpString = sb.toString().toLowerCase();
14. if(tmpString.startsWith("_")){
15. return tmpString.substring(1);
16. }else{
17. return tmpString;
18. }
19.}
1./**
2. * 去掉单词之间的下划线
3. */
4.private static String removeUnderline(String text) {
5. String regEx = "(_[a-z])";
6. Pattern p = Pattern.compile(regEx);
7. Matcher m = p.matcher(text.toLowerCase());
8. StringBuffer sb = new StringBuffer("");
9. while(m.find()){
10. m.appendReplacement(sb,m.group().replaceFirst("_","").toUpperCase());
11. }
12. m.appendTail(sb);
13. String ret = sb.toString();
14. String className = ret.toString().substring(0,1).toUpperCase() + ret.toString().substring(1);
15. return className;
16.}
1.//去掉字符串中的数字
2.String regEx = "(\\d)";
3.Pattern p = Pattern.compile(regEx);
4.Matcher m = p.matcher("word123something");
5.String ret = m.replaceAll("");
1570

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



