------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------
package com.itheimaday25;
/*
正则表达式:符合一定规则的表达式。
特点:用于一些特定的符号来标示一些代码操作。这样就简化书写。
所以学习正则表达式,就是在学习一些符号的使用!
好处:可以简化对字符串的复杂操作。
弊端:符号定义越多,正则越长,阅读性越差!
作用:用于专门操作字符串。
具体操作功能:
1,匹配:String类中的方法: boolean matches(String regex) 告知此字符串是否匹配给定的
用规则匹配整个字符串,只有一处不符合规则就匹配结束,返回false
2,切割:String split();
(String[] split(String regex, int limit) 根据匹配给定的正则表达式来拆分此字符串。 )
3,替换
String replace(CharSequence target, CharSequence replacement)
使用指定的字面值替换序列替换此字符串所有匹配字面值目标序列的子字符串。
String replaceAll(String regex, String replacement)
使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串。
*/
public class RegexDemo_01 {
public static void main(String[] args) {
// checkQQ();
// Demo();
// checkTel();
// splitDemo("zhangsan.wangwu.lisi",/*"[.]"*/"\\.");
// splitDemo("c:\\abc\\a.txt","\\\\");
// splitDemo("erkktyqqquizzzzzzodf", "(.)\\1+");
//按照叠词完成切割,为了让规则的结果被重用,可以将规则封装成一个组,用()完成。
//组的出现都有编号。从1开始。想要使用已有的组可以通过 \n(n就是组的编号)的形式来获取.
repliceAllDemo(
"vero187ty028jljajf61755daf", //将字符串中的数子替换成#
"\\d{3,}",
"#");
repliceAllDemo(
"erkktyqqquizzzzzzodf", //将叠词替换成&符号
//将重叠的字母替换成单个字母 aaa->a
// "(.)\\1+",
// "&");
"(.)\\1+",
"$1");
}
//替换演示:
public static void repliceAllDemo(String str,String reg,String newstr){
str =str.replaceAll(reg, newstr);
System.out.println(str);
}
//切割演示:
public static void splitDemo(String str, String reg){
// String reg =" +";//按照多个空格进行切割
// String reg =/*"[.]"*/"\\.";
String[] arr =str.split(reg);
System.out.println(arr.length);
for(String s:arr){
System.out.println(s);
}
}
/*
匹配:手机号段只有 13xxxxx 15xxxxx 18xxxx
*/
public static void checkTel(){
String tel = "18702861755";
String reg="[1][358]\\d{9}";
boolean flag = tel.matches(reg);
if(flag){
System.out.println("手机号:"+tel);
}else{
System.out.println("手机号码错误");
}
}
public static void Demo(){
String str = "c1";
String reg="[a-zA-z]\\d*";
boolean b = str.matches(reg);
System.out.println(b);
}
public static void checkQQ(){
String qq = "12345";
// boolean matches(String regex) 告知此字符串是否匹配给定的正则表达式。
String regex="[1-9]\\d{4,14}";
boolean flag = qq.matches(regex);
if(flag){
System.out.println(qq+"...is ok");
}else{
System.out.println(qq+"...QQ号不合法");
}
}
/*
* 对QQ代码进行校验
*
* 要求:5~15 0不能开头,只能是数字
*
* 这种方式,使用了string类中的方法,进行组合完成了需求。但是代码过于复杂。
*/
public static void checkQQ_1() {
String qq = "65234674321";
int len = qq.length();
if (len >= 5 && len <= 15) {
if (!(qq.startsWith("0"))) {
try {
long l=Long.parseLong(qq);
System.out.println("qq:"+l);
} catch (Exception e) {
throw new NumberFormatException("出现非法字符.......");
}
/* char[] arr = qq.toCharArray();
boolean flag = false;
for (int x = 0; x < arr.length; x++) {
if (!(arr[x] >= '0' && arr[x] <= '9')) {
flag = false;
break;
}
}
if (flag = true) {
System.out.println("qq:" + qq);
} else {
System.out.println("qq号出现非法字符");
}*/
} else {
System.out.println("QQ号码不可以以0开头");
}
} else {
System.out.println("qq号错误");
}
}
}
/*
正则表达式的第四个功能。
4,获取:将字符串中的符合规则的字串取出。
操作步骤:
1,将正则表达式封装成对象。
2,让正则表达式和 要操作的字符串相关联。
3,关联后,获取正则匹配引擎。
4,通过引擎对符合规则的字串进行操作,比如取出。
*/
(正则表达式常见操作)
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());
}
}
}
}
}
package com.itheimaday25;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/*
网页爬虫(蜘蛛)
*/
public class RegexTest_04 {
public static void main(String[] args) throws IOException {
getMails();
}
/*
获取指定文档中的邮件地址。使用获取功能,Pattern Matcher
*/
public static void getMails() throws IOException{
BufferedReader bufr = new BufferedReader(new FileReader("mail.txt"));
String line = null;
String mailereg ="\\w+@\\w+(\\.\\w+)+";
Pattern p = Pattern.compile(mailereg);
while((line=bufr.readLine())!=null){
Matcher m = p.matcher(line);
while(m.find()){
System.out.println(m.group());
}
}
bufr.close();
}
}