正则表达式:
符合一定规则的表达式。
作用:专门用于操作字符。
特点:用一些特定符号来表示一些代码操作。可以简化书写
所以学习正则表达式就是学习特殊符号的使用
好处:可以简化对字符串的复杂操作。
具体操作功能:
1.匹配:String matches方法
matches():用规则匹配整个字符串。
只要有一处不匹配返回false
2. 切割:String split(String regex);
3.替换 :String replaceAll();
代码演示:代码演示了替换字符串中数字为*(当然替换为什么可以自己定)
演示了切割以及校验QQ号和手机号等经典例子,可以细细体会正则语句,
package Regex;
public class RegexDemo {
public static void main(String[] args) {
//checkTel();
//checkQQ();
// splitDemo("zhangsan.lisi.wangwu.zhaoliu","\\.");
//String str = "sdfa1as2224155adf111412asdf2";//将字符串中数组替换成*
//replaceAllDemo(str,"\\d","*");//sdfa*as*******adf******asdf*
//replaceAllDemo(str,"\\d{1,}","*");//sdfa*as*adf*asdf*
// replaceAllDemo(str,"\\d{5,}","*");//sdfa1as*adf*asdf2
String str = "asfskkkasdfqqqqqqdfa";
// replaceAllDemo(str,"(.)\\1+","$1");//asfskasdfqdfa
replaceAllDemo(str,"(.)\\1+","*");//asfs*asdf*dfa
}
private static void replaceAllDemo(String str,String rex,String newStr) {
str = str.replaceAll(rex, newStr);
System.out.println(str);
}
//切割
private static void splitDemo(String str, String rex) {
/*String str = "";
String rex = "\\.";*/
String[] arr = str.split(rex);
for(String s : arr){
System.out.println(s);
}
}
//校验QQ号
private static void checkQQ() {
/*分析qq号一般是5-9位,不可以出现字母,开头不可以为0*/
String qq = "2780189437";
String rex = "[1-9]\\d{4,14}";
System.out.println(qq.matches(rex));
}
/*正则表达式匹配手机号:手机号开始一般以13xx 15xx 18xx
* 共11位 且全为数字。*/
public static void checkTel(){
String tel = "15110403105";
String rex = "1[358]\\d{9}";//第一位1第二位358中的一个,剩下9位全为数字即可。
System.out.println(tel.matches(rex));
}
}
正则表达式的第四个功能:
4. 获取:将字符串中符合规则的子串取出来
操作步骤:
1.将正则表达式封装成对象。
2.让正则对象和要操作的字符串相关联。
3.关联后。获取正则匹配引擎。
4.通过引擎对符合规则的子串进行操作,比如取出
代码示例:
import java.util.regex.*;
class RegexDemo2
{
public static void main(String[] args)
{
getDemo();
}
public static void getDemo()
{
//定义被操作的字符串
String str="ming tian jiu yao fang jia le ,da jia";
//定义规则\b单词边界
String reg="\\b[a-z]{3}\\b";
//将规则封装成对象。
Pattern p=Pattern.compile(reg);
//让正则对象和要作用的字符串进行关联,获取匹配器对象
Matcher m=p.matcher(str);
while(m.find())
{
System.out.println(m.group());
System.out.println(m.start()+"****"+m.end());
}
}
}
应用示例:使用正则表达式校验邮箱、对IP地址进行排序等
package Regex;
import java.util.TreeSet;
public class RegexTest {
public static void main(String[] args) {
//checkMail();
// test();
ipSort();
}/*192.168.0.102 102.59.23.013 10.10.10.10 2.2.2.2 8.109.109.50
将ip地址进行地址段顺序的排序。*/
private static void ipSort() {
String ip = "192.168.0.102 102.59.23.013 10.10.10.10 2.2.2.2 8.109.109.50";
ip = ip.replaceAll("(\\d+)", "00$1");//给所有的数字补零
System.out.println(ip);
ip = ip.replaceAll("0*(\\d{3})", "$1");//只保留三位数
System.out.println(ip);
String[] arr = ip.split(" ");//用空格符分割
TreeSet<String> ts = new TreeSet<String>();
for(String s : arr){
ts.add(s);//放入到TreeSet集合进行默认排序。
}
for(String s : ts){//去掉多余的0并按照默认排序取出打印
System.out.println(s.replaceAll("0*(\\d)", "$1"));
}
}
//"我我...我我..我要..要要要....学学学...学学...编编编编...编编....程程程...程"
//我要学编程。
private static void test() {
String str = "我我...我我..我要..要要要....学学学...学学...编编编编...编编....程程程...程";
str = str.replaceAll("\\.+","");
System.out.println(str);
str = str.replaceAll("(.)\\1+", "$1");
System.out.println(str);
}
private static void checkMail() {
String mail = "y18330025930@163.com.cn";
String rex = "[a-zA-Z0-9_]+@[a-zA-Z0-9]+(\\.[a-zA-Z]+){1,3}";
System.out.println(mail.matches(rex));
}
}
此处知识几个简单的使用正则表达式的小例子,要多练才能熟练掌握。加油吧!