“String类提供的正则表达式应用”的版本间的差异
跳到导航
跳到搜索
Jihongchang(讨论 | 贡献) (→案例) |
Jihongchang(讨论 | 贡献) (→示例2:) |
||
第80行: | 第80行: | ||
} | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | === 判断是否匹配 === | ||
+ | <syntaxhighlight lang="java"> | ||
+ | package java.lang; | ||
+ | …… | ||
+ | public final class String | ||
+ | implements java.io.Serializable, Comparable<String>, CharSequence { | ||
+ | |||
+ | …… | ||
+ | public boolean matches(String regex) { | ||
+ | return Pattern.matches(regex, this); | ||
+ | } | ||
+ | |||
+ | …… | ||
+ | |||
+ | } | ||
+ | </syntaxhighlight>要求验证一个手机号必须是 138 或139 开头的11位数字<syntaxhighlight lang="java"> | ||
+ | public class StringReg1 { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | String content = "13811663445"; | ||
+ | |||
+ | String content2 = "1381166344"; //138开头、不足11位的 | ||
+ | |||
+ | String content3 = "13711663445"; //非138开头、11位 | ||
+ | |||
+ | String content4 = "1381166344"; //非138开头、不足11位 | ||
+ | |||
+ | String regex = "^13[8|9]\\d{8}"; | ||
+ | |||
+ | System.out.println("content.matches(regex):" + content.matches(regex)); | ||
+ | |||
+ | System.out.println("content2.matches(regex):" + content2.matches(regex)); | ||
+ | |||
+ | System.out.println("content3.matches(regex):" + content3.matches(regex)); | ||
+ | |||
+ | System.out.println("content4.matches(regex):" + content4.matches(regex)); | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | </syntaxhighlight><syntaxhighlight lang="console"> | ||
+ | content.matches(regex):true | ||
+ | content2.matches(regex):false | ||
+ | content3.matches(regex):false | ||
+ | content4.matches(regex):false | ||
</syntaxhighlight> | </syntaxhighlight> |
2022年11月20日 (日) 10:38的版本
https://www.bilibili.com/video/BV1Eq4y1E79W?p=23
String类中使用正则表达式
String.java 源代码
package java.lang;
……
public final class String
implements java.io.Serializable, Comparable<String>, CharSequence {
……
public String replaceAll(String regex, String replacement) {
return Pattern.compile(regex).matcher(this).replaceAll(replacement);
}
……
}
匹配替换
2000年5月,JDK1.3、JDK1.4和J2SE1.3相继发布,J2SE1.3是对J2SE1.2的补充和扩展,从应用领域的角度分析,JavaSE1.3已经涵盖了数据库、WEB、网络、图形、多媒体、电话、影像等大部分的信息技术领域。
2002年2月26日,J2SE1.4发布。与J2SE1.3相比,多了近62%的类和接口。在这些新特性当中,还提供了广泛的XML支持、安全套接字(Socket)支持(通过SSL与TLS协议)、全新的I/OAPI、正则表达式、日志与断言。
将上面文字 JDK1.3 JDK1.4 统一替换成 JDK
示例1:
public class StringReg {
public static void main(String[] args) {
String content = "2000年5月,JDK1.3、JDK1.4和J2SE1.3相继发布,J2SE1.3是对J2SE1.2的补充和扩展,从应用领域的角度分析,JavaSE1" +
".3已经涵盖了数据库、WEB、网络、图形、多媒体、电话、影像等大部分的信息技术领域。\n" +
"\n" +
"2002年2月26日,J2SE1.4发布。与J2SE1.3相比,多了近62%的类和接口。在这些新特性当中,还提供了广泛的XML支持、安全套接字(Socket)支持(通过SSL与TLS协议)、全新的I" +
"/OAPI、正则表达式、日志与断言。";
String regex = "(JDK)1\\.[3|4]";
String ret = content.replaceAll(regex, "$1");
System.out.println(ret);
}
}
2000年5月,JDK、JDK和J2SE1.3相继发布,J2SE1.3是对J2SE1.2的补充和扩展,从应用领域的角度分析,JavaSE1.3已经涵盖了数据库、WEB、网络、图形、多媒体、电话、影像等大部分的信息技术领域。
2002年2月26日,J2SE1.4发布。与J2SE1.3相比,多了近62%的类和接口。在这些新特性当中,还提供了广泛的XML支持、安全套接字(Socket)支持(通过SSL与TLS协议)、全新的I/OAPI、正则表达式、日志与断言。
示例2:
public class StringReg {
public static void main(String[] args) {
String content = "2000年5月,JDK1.3、JDK1.4和J2SE1.3相继发布,J2SE1.3是对J2SE1.2的补充和扩展,从应用领域的角度分析,JavaSE1" +
".3已经涵盖了数据库、WEB、网络、图形、多媒体、电话、影像等大部分的信息技术领域。\n" +
"\n" +
"2002年2月26日,J2SE1.4发布。与J2SE1.3相比,多了近62%的类和接口。在这些新特性当中,还提供了广泛的XML支持、安全套接字(Socket)支持(通过SSL与TLS协议)、全新的I" +
"/OAPI、正则表达式、日志与断言。";
String regex = "JDK1\\.3|JDK1\\.4";
String ret = content.replaceAll(regex, "JDK");
System.out.println(ret);
}
}
判断是否匹配
package java.lang;
……
public final class String
implements java.io.Serializable, Comparable<String>, CharSequence {
……
public boolean matches(String regex) {
return Pattern.matches(regex, this);
}
……
}
要求验证一个手机号必须是 138 或139 开头的11位数字
public class StringReg1 {
public static void main(String[] args) {
String content = "13811663445";
String content2 = "1381166344"; //138开头、不足11位的
String content3 = "13711663445"; //非138开头、11位
String content4 = "1381166344"; //非138开头、不足11位
String regex = "^13[8|9]\\d{8}";
System.out.println("content.matches(regex):" + content.matches(regex));
System.out.println("content2.matches(regex):" + content2.matches(regex));
System.out.println("content3.matches(regex):" + content3.matches(regex));
System.out.println("content4.matches(regex):" + content4.matches(regex));
}
}
content.matches(regex):true
content2.matches(regex):false
content3.matches(regex):false
content4.matches(regex):false