package test;
import java.util.Arrays;
public class test {
public static void main(String[] args) {
String str = "100034234,100234,12,1234,1124,124";
String regex = ",";
String result[] = str.split(regex);
System.out.println(Arrays.asList(result));
}
}
输出:[100034234, 100234, 12, 1234, 1124, 124]
package test;
import java.util.Arrays;
public class test {
public static void main(String[] args) {
String str = "zhangsan lisi wangwu";
String regex = " +";// 表示空格出现一次或多次
String result[] = str.split(regex);
System.out.println(Arrays.asList(result));
}
}
输出:[zhangsan, lisi, wangwu]
package test;
import java.util.Arrays;
public class test {
public static void main(String[] args) {
String str = "zhangsan.lisi.wangwu";
String regex = "\\.";// .为特殊字符,匹配所有字符,\.表示匹配.,\\.表示字符串中匹配
String result[] = str.split(regex);
System.out.println(Arrays.asList(result));
}
}
输出:[zhangsan, lisi, wangwu]
package test;
import java.util.Arrays;
public class test {
public static void main(String[] args) {
String str = "c:\\abc\\t.txt";
String regex = "\\\\";// 表示匹配两个\
String result[] = str.split(regex);
System.out.println(Arrays.asList(result));
}
}
输出:[c:, abc, t.txt]
package test;
import java.util.Arrays;
public class test {
public static void main(String[] args) {
String str = "abccdeffghi00klmAAopq";
String regex = "(.)\\1";// 按照叠词完成切割,为了可以让规则的结果重用,可以将规则封装成一个组,用()完成,组的编号从1开始,通过/n使用已有的组
String result[] = str.split(regex);
System.out.println(Arrays.asList(result));
}
}
输出:[ab, de, ghi, klm, opq]
package test;
import java.util.Arrays;
public class test {
public static void main(String[] args) {
String str = "abcccccccccccdefffghi0000klmAAAAAAopq";
String regex = "(.)\\1+";
String result[] = str.split(regex);
System.out.println(Arrays.asList(result));
}
}
输出:[ab, de, ghi, klm, opq]
package test;
public class test {
public static void main(String[] args) {
String str = "sadf123276rfkgfjk989956jkj45783fnmd334";
String regex = "\\d{5,}";// 将5个以上的数字替换
String result = str.replaceAll(regex, "#");
System.out.println(result);
}
}
输出:sadf#rfkgfjk#jkj#fnmd334
package test;
public class test {
public static void main(String[] args) {
String str = "dsnmsssjkkjfduuuuukhkjhddddkjk";
String regex = "(.)\\1+";// 匹配重叠的字符
String result = str.replaceAll(regex, "$1");// 将重叠的的字符替换为单个字符 zzzzzz->z
System.out.println(result);
}
}
输出:dsnmsjkjfdukhkjhdkjk
package test;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class test {
public static void main(String[] args) {
String str = "ming tian jiu yao fang jia le , da jia. ";
String regex = "\\b[a-z]{4}\\b";// \b为匹配单词边界,此正则匹配4位字符的边界
Pattern p = Pattern.compile(regex);// 将规则封装成对象
Matcher m = p.matcher(str);// 将正则对象和要作用的字符串相关联,获取匹配器对象
while (m.find()) {
System.out.println(m.group());
System.out.println(m.start() + "....." + m.end());
}
}
}
输出:
ming
0.....4
tian
5.....9
fang
18.....22
package test;
public class test {
public static void main(String[] args) {
String str = "20140108";
String regex = "(\\d{4})(\\d{2})(\\d{2})";
String result = str.replaceAll(regex, "$1-$2-$3");
System.out.println(result);
}
}
输出:2014-01-08