面向对象
正则表达式
(概述)
/*
正则表达式:符合一定规则的表达式。
作用:用于专门操作字符串。
特点:用于一些特定的符号来表示一些代码操作。这样就简化书写。
所以学习正则表达式,就是在学习一些特殊符号的使用。
*/
好处:可以简化对字符串的复杂操作。
弊端:符号定义越多,正则越长,阅读性越差。
规律:在正则表达式中如果出现\那\\必须是成对出现的。
(正则表达式常见操作)
1,匹配:String matches方法。用规则匹配整个字符串,只要有一处不符合规则,就匹配结束,返回false。
2,切割:String split();
3,替换:String replaceAll(regex,str);如果regex中有定义组,可以在第二参数中通过$符号获取正则表达式中的已有的组。
(正则表达式——匹配-切割-替换)
public class Test{
public static void main(String...args){
//oldCheckQQ("12345");
//checkQQ("01234");
//split("192.168.9.101");
//replaceAll("heima#163.com");
}
public static void replaceAll(String E_mail){
String regex ="[#]";
String newStr = E_mail.replaceAll(regex,"@");
System.out.println(newStr);
}
public static void split(String ip){
String regex ="\\.";
String[] arr = ip.split(regex);
for(StringsubString :arr){
System.out.println(subString);
}
}
public static void checkQQ(String qq){
String regex ="[1-9]\\d{4,9}";
boolean flag = qq.matches(regex);
if(flag)
System.out.println(qq+"is ok");
else
System.out.println("I don't no");
}
public static void oldCheckQQ(String qq){
int len = qq.length();
if(len>=5&&len<=10){
if(!(qq.startsWith("0"))){
try{
long l =Long.parseLong(qq);
System.out.println("登录成功");
}
catch (Exception e){
System.out.println("登录失败");
}
/*
char[] chs = qq.toCharArray();
boolean falg = true;
for(char ch:chs){
if(!(ch>='0'&&ch<='9')){
falg = false;
break;
}
}
if(falg){
System.out.println("登录成功");
}
else{
System.out.println("登录失败");
}
}*/
else{
System.out.println("不能0开头");
}
}
else{
System.out.println("长度错误");
}
}
}
(正则表达式——获取)
/*
正则表达式的第四个功能。
4,获取:将字符串中的符合规则的子串取出。
操作步骤:
1,将正则表达式封装成对象。
2,让正则对象和要操作的字符串相关联。
3,关联后,获取正则匹配引擎。
4,通过引擎对符合规则的子串进行操作,比如取出。
*/
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Test{
public static void main(String...args){
get("ming tian jiu yao fang jia le");
}
public static void get(String str){
String regex ="\\b[a-z]{4}\\b";
Pattern p =Pattern.compile(regex);
Matcher m = p.matcher(str);
System.out.println(m.matches());
//System.out.println(m.find());
//System.out.println(m.group());
while(m.find()){
System.out.println(m.group());
System.out.println(m.start()+"..."+m.end());
}
}
}
(正则表达式——综合应用练习)
/*
到底用四种功能中的哪一个呢?或者哪几个呢?
思路方式:
1,如果只想知道该字符是否对是错,使用匹配。
2,想要将已有的字符串变成另一个字符串,替换。
3,想要按照自定的方式将字符串变成多个字符串。切割。获取规则以外的子串。
4,想要拿到符合需求的字符串子串,获取。获取符合规则的子串。
*/
/*
需求:
将下列字符串转成:我要学编程.
思路:
1.先把所有点去掉。可以使用替换方法
2.把连续的字符换成一个。可以使用替换方法
3.OK
*/
public classTest{
public static void main(String...args){
String str ="我我我....要..要要...学..学.编...编编编编...编....程程程...程...";
str = str.replaceAll("\\.","");
String newstr = str.replaceAll("(.)\\1+","$1");
System.out.println(newstr);
}
}
/*
需求:
对邮箱地址进行校验。
思路:
1.先把邮箱固定的字符确定出来。
2.头不可以0开头和_下划线。
3.邮箱的长度不可以小于5,并且得小于12.。
4.邮箱的后缀名可以是.com或者.com.cn
5.OK
*/
public class Test{
public static void main(String...args){
String mail ="987688882@sina.com.cn";
String regex ="[1-9a-zA-Z]\\w+@\\w+(\\.\\w+)+";
boolean falg = mail.matches(regex);
if(falg)
System.out.println("发送成功");
else
System.out.println("发送失败");
}
}
/*
需求:
获取一个网页上的邮箱地址
*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.net.URL;
public classTest{
public static void main(String...args)throws Exception{
URL url =new URL("http://127.0.0.1:8080/");
BufferedReader bufin =
new BufferedReader(new InputStreamReader(url.openStream()));
String line =null;
String regex = "[1-9a-zA-z]\\w+@\\w+(\\.\\w+)+";
Pattern p =Pattern.compile(regex);
while((line=bufin.readLine())!=null){
Matcher m = p.matcher(line);
while(m.find()){
System.out.println(m.group());
}
}
}
}