JAVASE学习笔记第25天
来源:互联网 发布:尔雅网络课是什么 编辑:程序博客网 时间:2024/06/05 07:46
正则表达式(入门)
/**
正册表达式:符合一定规则的表达式
作用:用于专门操作字符串
class Demo1
{
publicstatic void main(String[] args)
{
chekQQ();
}
对QQ号码进行校验,
要求:5~15位,0不能开头,只能是数字
publicstatic void chekQQ()
{
Stringqq="123454d";
intlen=qq.length();
if(len>=5&& len<15)
{
if(!qq.startsWith("0"))
{
/*
char[]arr=qq.toCharArray();
booleanflag=true;
for(intx=0;x<arr.length;x++)
{
if(!(arr[x]>='0'&& arr[x]<='9'))
{
flag=false;
break;
}
}
if(flag)
{
System.out.println("qq:"+qq);
}
else
{
System.out.println("这是非法字符");
}
}
//Integer.parseInt("12a");出现NumberFormatException
try
{
longl =Long.parseLong(qq);
System.out.println("qq:"+qq);
}
catch(NumberFormatException e)
{
System.out.println("出现非法字符");
}
}
else
{
System.out.println("不可是0开头");
}
}
else
{
System.out.println("长度不符合");
}
}
}
以上的方法过于复杂,使用正则表达式
*/
class Demo1
{
publicstatic void main(String[] args)
{
Stringqq="1234516";
Stringregex="[1-9][0-9]{4,14}";
booleanflag=qq.matches(regex);
if(flag)
System.out.println(qq+"ccc");
else
System.out.println(qq+"不合法");
}
}
正则表达式(四种功能)
/*
正则表达式:符合一定规则的表达式
作用:专门用来操作字符串
特点: 用一些特定的符号来表示一些代码,这样就简化了书写。
所以学习正册表达式就是在学习一些符号的应用。
好处:可以简化对字符串的复杂的操作。
弊端:符号定义越多,正册表达式也就越复杂,不易阅读。
具体的操作功能:
1,匹配;Stringmatches方法
2,切割:Stringsplit
3,替换:replaseAll();
*/
class Demo2
{
public static void main(String[] args)
{
//checkTel();
//splitDemo();
//将字符串中的数字替换为#号
Stringstr="qegrsdgfgm13563727070897ufsfsdfsfs";
replaceAllDemo(str,"\\d{5}","#");
//将叠词替换为@
Stringstr1="erkkgegeooddm";
replaceAllDemo(str1,"(.)\\1+","@");
//将重叠的字母替换为单个字母,例如:将kkk-->k
//$1是或缺到分组
replaceAllDemo(str1,"(.)\\1+","$1");
}
public static void demo()
{
String str="bA";
String reg = "[bcd][a-z]";
boolean b =str.matches(reg);
System.out.println(b);
}
/*
手机号段都是13XXX15XXx 18xxx
*/
public static void checkTel()
{
String tel="13000001111";
String telReg="1[358]\\d{9}";
System.out.println(tel.matches(telReg));
}
publicstatic void splitDemo()
{
Stringstr="张三 李四 王五";
//Stringreg=" +"; //空格出现多次
//取得a.txt
//str="C:\\abc\\a.txt";
//Stringreg="\\\\";
//.在正册表达式中表示任意字符,这时候的表示形式外为:\\.
//Stringreg="\\.";
//切割叠词
/*
按照叠词切割,为了可以让正册的结果被重用,
可以将规则组成一个组。用()完成。组出现的编号从1开始
想要使用已经有的组可以通过\n(n就是组的编号)的形式来获取。
*/
str="3ett40404";
Stringreg="(.)\\1+";
String[]arr=str.split(reg);
for(Strings: arr)
{
System.out.println(s);
}
}
//替换
publicstatic void replaceAllDemo(String str,String reg,String newStr)
{
str=str.replaceAll(reg,newStr);
System.out.println(str);
}
}
/*
匹配:返回的是真假
切割:符合规则以外的
替换:替换后
正则表达式的第四个功能:获取
将字符串中符合规则的子串取出
操作步骤:
1,将正则表达式封装为对象
2,让正则对象和要操作的字符串相关联
3,关联后,获取正则匹配引擎。
4,通过引擎对符合规则的子串进行操作,比如取出。
*/
import java.util.regex.*;
class Demo4
{
publicstatic void main(String[] args)
{
getDemo();
}
publicstatic void getDemo()
{
Stringstr="ming tian jiu yao fang jia la";
Stringreg="\\b[a-z]{4}\\b"; //\b是单词结尾
//1,将规则封装为对象
Patternp = Pattern.compile(reg);
//2,将正则对象和要操作的字符串相关联,获取匹配器对象(也就是引擎)
Matcherm =p.matcher(str);
//3,通过引擎对符合规则的字符串进行操作
while(m.find())
{
System.out.println(m.group());
}
}
}
正则表达式(练习)
/*
在选择功能的时候,我们如何选择呢?
1,如果只是想知道该字符串是对是错,那么使用匹配
2,想要将已经有的字符串转换为另外一个字符串的话,使用替换
3,想要按照自己自定义的方式将一个字符串转成为多个字符串,切割。获取规则以外的子串
4,想要拿到符合要求的字符串的子串,获取。获取符合符合规则的子串。
*/
import java.util.*;
class Demo5
{
public static voidmain(String[] args)
{
//demo1();
//ipSort();
checkMail();
}
//需求:将已经给了的字符串转换为:我要学编程
public static voiddemo1()
{
String str="我我……我我……我要……要要……学学……学学……学编程";
/*
思路:使用替换
1,将.去掉
2,将多个重复的内容变换为单个内容。
*/
str=str.replaceAll("\\…+","");
System.out.println(str);
str=str.replaceAll("(.)\\1+","$1");
System.out.println(str);
}
/*
需求2:
192.68.1.254102.49.23.013 10.10.10.10 2.2.2.2 8.109.3.2
将ip地址进行地址段的排序
思路:
还是按照字符串的自然顺序,只要让他们的每一段都是3位即可
1,按照每一段所需要的最多的0进行补齐,保证每一段至少是3位
2,将每一段只保留3位。这样,所有的ip地址都是每一段3位
*/
public static voidipSort()
{
Stringip="192.68.1.254 102.49.23.013 10.10.10.10 2.2.2.2 8.109.3.2 ";
//加0
ip=ip.replaceAll("(\\d+)","00$1");
System.out.println(ip);
//将多余的0去掉
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);
}
for(String s:ts)
{
System.out.println(s.replaceAll("0*(\\d+)","$1"));
}
}
/*
需求:对邮件进行校验(这个必须掌握)
可以先找固定的东西
*/
public static voidcheckMail()
{
String mail ="abc12@163.com";
//精确的规则
Stringreg="[a-zA-Z0-9_]{6,12}@[a-zA-Z0-9_]+(\\.[a-zA-Z]+)";
//不精确
reg="\\w+@\\w+(\\.\\w+)+";
System.out.println(mail.matches(reg));
}
}
正则表达式(网页爬虫)
/*
网页爬虫(蜘蛛)
*/
class Demo6
{
publicstatic void main(String[] args)
{
getMails();
}
/*
获取指定文档中的邮件地址。
使用获取功能。PatternMatcher
*/
publicstatic void getMails()throws Exception
{
BufferedReaderbufr=
newBufferedReader(new FileReader("mail.txt"));
Stringline =null;
StringmailReg="\\w+@\\w+(\\.\\w+)+";
Patternp =Pattern.compile(mailReg);
while((line=bufr.readLine())!=null)
{
Matcherm =p.matcher(line);
while(m.find())
{
System.out.println(m.group());
}
}
}
publicstatic void getMails1()throws Exception
{
URLurl=new URL("http://192.168.1.254:8080/mail.txt");
URLConnectionconn=url.openConnection();
BufferedReaderbufIn=
newBufferedReader(new InputStream(conn.getInputStream()));
Stringline=null;
StringmailRge ="\\w+@\\w+(\\.\\w+]+)";
Patternp = Pattern.compile(mailReg);
while((line=bufIn.readLine())!=null)
{
Matcherm =p.matcher(line);
while(m.find())
{
System.out.println(m.group());
}
}
}
}
- JAVASE学习笔记第25天
- JAVASE学习笔记第5天
- JAVASE学习笔记第6天
- JAVASE学习笔记第7天
- JAVASE学习笔记第8天
- JAVASE学习笔记第9天
- JAVASE学习笔记第10天
- JAVASE学习笔记第11天
- JAVASE学习笔记第12天
- JAVASE学习笔记第13天
- JAVASE学习笔记第14天
- JAVASE学习笔记第15天
- JAVASE学习笔记第16天
- JAVASE学习笔记第17天
- JAVASE学习笔记第18天
- JAVASE学习笔记第19天
- JAVASE学习笔记第20天
- JAVASE学习笔记第21天
- vlan并不适合大规模的cloud
- 23种设计模式全解析
- JAVASE学习笔记第24天
- Uncompressing Linux... done, booting the kernel
- 开始计算机网络之旅
- JAVASE学习笔记第25天
- 链表相关面试题目 反转 合并
- C++ Primer笔记 模板
- 后台管理框架之三 :整体构建
- 分支管理
- Java TCP使用Socket进行网络文件传送(5)
- 设计模式--singleton模式
- nginx 404页面处理以及pathInfo和隐藏index.php总述
- 模拟实现C语言库函数