黑马程序员—正则表达式概念
---------------------- <a href="http://www.itheima.com"target="blank">ASP.Net+Unity开发</a>、<a href="http://www.itheima.com"target="blank">.Net培训</a>、期待与您交流! ------
一.概念:
正则表达式:用于专门操作字符串。可以简化对只付出的复杂操作。
1.特点:通过特点的字符来体现的, [^abc] ,[ \t\n\x0B\f\r]
2.弊端:掌握正则表达式必须学习一些符合,定义越多 阅读性差,而且不好记忆理解容易混淆。
package com.itheima.regex.demo;
public class RegexDemo {
/**
* @param args
*/
public static void main(String[] args) {
String id="42112419890304";
//checkId(id);
checkId2(id);
}
/**
* 需求:假如检核一个人身份编号,只能是数字组成,而且不能为0开头,长度10到18位
*/
public static void checkId(String id){
int len=id.length(); //获取长度
if(len>=10 && len<=18){
if(id.startsWith("0")){
System.out.println("编号不能以0开头");
}else{
try {
long Id=Long.getLong(id);
System.out.print("id编号是:"+Id);
} catch (Exception e) {
System.out.println("出现非法字符。");
}
}
}else{
System.out.println("身份证编号长度不符合要求");
}
}
//使用正则表达式判断
public static void checkId2(String id) {
// 定义正则表达式规则:第一位是1-9中的一个,然后0-9中的数字出现10-17次。
String regex = "[1-9][0-9]{9,17}" ; //定义规则
boolean flag = id.matches(regex) ;
if (flag)
System.out.println(id+":此号码正确");
else
System.out.println(id+":此编号不合法");
}
}
二、正则表达式具体操作:
package com.itheima.regex.demo;
public class RegexDemo2 {
/**
* @param args
*/
public static void main(String[] args) {
//functionDmeo1();
//functionDmeo2();
functionDmeo3();
}
//匹配手机号码:通常手机号码是11位,而开头一般是13、15、18。
public static void functionDmeo1(){
String tel="13632979740";
String regex="1[358]\\d{9}"; //\d 数字:[0-9]
Boolean b=tel.matches(regex);
System.out.print("手机号码:"+b);
}
//切割: String[] split(String regex) ; 按照切割规则将字符串切割成多个子串。
// 按照定义的规则进行切割
public static void functionDmeo2(){
String str="hao123ddwww360qq";
String [] names=str.split("(.)\\1+");
for(String name:names){
System.out.print(name);
}
}
//替换 replace
public static void functionDmeo3(){
String str="hao123ddwww360qq";
str=str.replaceAll("(.)\\1+", "$1");
System.out.print(str);
}
}
1、匹配: boolean matches(String regex) 用规则匹配整个字符串,只要有一处不符合规则就返回false。
总结:如果只想知道该字符串是对是错,使用匹配。
2、切割: String[] split(String regex) ; 按照切割规则将字符串切割成多个子串。
总结:想要按照自定的方式将字符串变成多个字符串,切割。获取规则以外的子串。
3、替换:boolean replaceAll(String regex, String replacement) 使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串。
总结:想要将已有的字符串变成另一个字符串, str=str.replaceAll("(.)\\1+", "$1")则使用替换。
练习题1:将字符串转换为:我要学编程开发
"我我我我...我要...要要要...学学..学编..编编编编编编编....编程.程...程程程..程.....开开开....发发发.发"
思路:先去掉".",然后将重叠字替换。
public static void Test_1(){
String str="我我我我...我要...要要要...学学..学编..编编编编编编编....编程.程...程程程..程.....开开开....发发发.发";
//将字符中的.去掉,用替换
str=str.replaceAll("\\.+", "");
//System.out.println(str);
//2.替换相同的叠词
str=str.replaceAll("(.)\\1+", "$1");
System.out.println(str);
}
练习题2:将IP地址进行排序 如: 192.168.1.234 102.49.23.13 16.10.21.10 1.1.1.1 8.109.90.55
分析: 1、让IP每一段位数相同,按照每一段需要的最多的0进行补齐,所以每一位至少有3位。
2、然后将每一段只保留3位。
3、切割出每一个IP地址,去掉0开头。
public static void Test_2(){
String ip_str="192.168.1.234 102.49.23.13 16.10.21.10 10.2.36.72 8.109.90.55";
//1.先补零,最多补2个
ip_str=ip_str.replaceAll("(\\d+)", "00$1");
System.out.println("每段补齐0后:"+ip_str);
//00192.00168.001.00234 00102.0049.0023.0013 0016.0010.0021.0010 0010.002.0036.0072 008.00109.0090.0055
//2。每段补齐后保留3位数
ip_str= ip_str.replaceAll("0*(\\d{3})", "$1") ;
System.out.println("去0后:"+ip_str);
//3.切割,将Ip切出
String [] ips=ip_str.split(" +");
TreeSet<String> ts=new TreeSet<String>();
for(String ip:ips){
ts.add(ip);
}
for(String ip:ts){
System.out.println(ip.replaceAll("0*(\\d+)", "$1"));
}
}
每段补齐0后:00192.00168.001.00234 00102.0049.0023.0013 0016.0010.0021.0010 0010.002.0036.0072 008.00109.0090.0055
去0后:192.168.001.234 102.049.023.013 016.010.021.010 010.002.036.072 008.109.090.055
8.109.90.55
10.2.36.72
16.10.21.10
102.49.23.13
192.168.1.234
正则表达式总结:正则表达式符号比较多,容易混淆,前几天敲的估计过一段时间忘记,这个开发中前段Javascript判断登录验证应该用的比较多,学习的是一种思路,这表达式符号用的时候可以查阅,死记硬背是没用用的,而且是痛苦的。
---------------------- <a href="http://www.itheima.com"target="blank">ASP.Net+Unity开发</a>、<a href="http://www.itheima.com"target="blank">.Net培训</a>、期待与您交流! -------------------------------
---------------------- <a href="http://www.itheima.com"target="blank">ASP.Net+Unity开发</a>、<a href="http://www.itheima.com"target="blank">.Net培训</a>、期待与您交流! ------
一.概念:
正则表达式:用于专门操作字符串。可以简化对只付出的复杂操作。
1.特点:通过特点的字符来体现的, [^abc] ,[ \t\n\x0B\f\r]
2.弊端:掌握正则表达式必须学习一些符合,定义越多 阅读性差,而且不好记忆理解容易混淆。
package com.itheima.regex.demo;
public class RegexDemo {
/**
* @param args
*/
public static void main(String[] args) {
String id="42112419890304";
//checkId(id);
checkId2(id);
}
/**
* 需求:假如检核一个人身份编号,只能是数字组成,而且不能为0开头,长度10到18位
*/
public static void checkId(String id){
int len=id.length(); //获取长度
if(len>=10 && len<=18){
if(id.startsWith("0")){
System.out.println("编号不能以0开头");
}else{
try {
long Id=Long.getLong(id);
System.out.print("id编号是:"+Id);
} catch (Exception e) {
System.out.println("出现非法字符。");
}
}
}else{
System.out.println("身份证编号长度不符合要求");
}
}
//使用正则表达式判断
public static void checkId2(String id) {
// 定义正则表达式规则:第一位是1-9中的一个,然后0-9中的数字出现10-17次。
String regex = "[1-9][0-9]{9,17}" ; //定义规则
boolean flag = id.matches(regex) ;
if (flag)
System.out.println(id+":此号码正确");
else
System.out.println(id+":此编号不合法");
}
}
二、正则表达式具体操作:
package com.itheima.regex.demo;
public class RegexDemo2 {
/**
* @param args
*/
public static void main(String[] args) {
//functionDmeo1();
//functionDmeo2();
functionDmeo3();
}
//匹配手机号码:通常手机号码是11位,而开头一般是13、15、18。
public static void functionDmeo1(){
String tel="13632979740";
String regex="1[358]\\d{9}"; //\d 数字:[0-9]
Boolean b=tel.matches(regex);
System.out.print("手机号码:"+b);
}
//切割: String[] split(String regex) ; 按照切割规则将字符串切割成多个子串。
// 按照定义的规则进行切割
public static void functionDmeo2(){
String str="hao123ddwww360qq";
String [] names=str.split("(.)\\1+");
for(String name:names){
System.out.print(name);
}
}
//替换 replace
public static void functionDmeo3(){
String str="hao123ddwww360qq";
str=str.replaceAll("(.)\\1+", "$1");
System.out.print(str);
}
}
1、匹配: boolean matches(String regex) 用规则匹配整个字符串,只要有一处不符合规则就返回false。
总结:如果只想知道该字符串是对是错,使用匹配。
2、切割: String[] split(String regex) ; 按照切割规则将字符串切割成多个子串。
总结:想要按照自定的方式将字符串变成多个字符串,切割。获取规则以外的子串。
3、替换:boolean replaceAll(String regex, String replacement) 使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串。
总结:想要将已有的字符串变成另一个字符串, str=str.replaceAll("(.)\\1+", "$1")则使用替换。
练习题1:将字符串转换为:我要学编程开发
"我我我我...我要...要要要...学学..学编..编编编编编编编....编程.程...程程程..程.....开开开....发发发.发"
思路:先去掉".",然后将重叠字替换。
public static void Test_1(){
String str="我我我我...我要...要要要...学学..学编..编编编编编编编....编程.程...程程程..程.....开开开....发发发.发";
//将字符中的.去掉,用替换
str=str.replaceAll("\\.+", "");
//System.out.println(str);
//2.替换相同的叠词
str=str.replaceAll("(.)\\1+", "$1");
System.out.println(str);
}
练习题2:将IP地址进行排序 如: 192.168.1.234 102.49.23.13 16.10.21.10 1.1.1.1 8.109.90.55
分析: 1、让IP每一段位数相同,按照每一段需要的最多的0进行补齐,所以每一位至少有3位。
2、然后将每一段只保留3位。
3、切割出每一个IP地址,去掉0开头。
public static void Test_2(){
String ip_str="192.168.1.234 102.49.23.13 16.10.21.10 10.2.36.72 8.109.90.55";
//1.先补零,最多补2个
ip_str=ip_str.replaceAll("(\\d+)", "00$1");
System.out.println("每段补齐0后:"+ip_str);
//00192.00168.001.00234 00102.0049.0023.0013 0016.0010.0021.0010 0010.002.0036.0072 008.00109.0090.0055
//2。每段补齐后保留3位数
ip_str= ip_str.replaceAll("0*(\\d{3})", "$1") ;
System.out.println("去0后:"+ip_str);
//3.切割,将Ip切出
String [] ips=ip_str.split(" +");
TreeSet<String> ts=new TreeSet<String>();
for(String ip:ips){
ts.add(ip);
}
for(String ip:ts){
System.out.println(ip.replaceAll("0*(\\d+)", "$1"));
}
}
每段补齐0后:00192.00168.001.00234 00102.0049.0023.0013 0016.0010.0021.0010 0010.002.0036.0072 008.00109.0090.0055
去0后:192.168.001.234 102.049.023.013 016.010.021.010 010.002.036.072 008.109.090.055
8.109.90.55
10.2.36.72
16.10.21.10
102.49.23.13
192.168.1.234
正则表达式总结:正则表达式符号比较多,容易混淆,前几天敲的估计过一段时间忘记,这个开发中前段Javascript判断登录验证应该用的比较多,学习的是一种思路,这表达式符号用的时候可以查阅,死记硬背是没用用的,而且是痛苦的。
---------------------- <a href="http://www.itheima.com"target="blank">ASP.Net+Unity开发</a>、<a href="http://www.itheima.com"target="blank">.Net培训</a>、期待与您交流! -------------------------------