Java基础知识

一、匹配

1public class RegularExpressionDemo{ 2 public static void main(String[] args){ 3 4 //匹配电话号码 5 String telphoneNum= "0015012828944"; 6 String reg= "^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\\d{8}$"; 7 System.out.println(telphoneNum.matches(reg)); 8 9 } 10}

二、切割

1public class RegularExpressionDemo{ 2 public static void main(String[] args){ 3 4 //把语句切割为单词 5 String sentence = "welcome to china "; 6 String reg = " +"; 7 String[] words = sentence.split(reg); 8 for(String word : words){ 9 System.out.println(word); 10 } 11 } 12}

三、替换

1public class RegularExpressionDemo{ 2 public static void main(String[] args){ 3 4 //把字符串中的标点符号换成空 5 String sentence = "How are you? Fine, thanks."; 6 String reg = "[?,.]"; 7 sentence = sentence.replaceAll(reg,""); 8 System.out.println(sentence); 9 } 10}

四、获取

1import java.util.regex.Pattern; 2import java.util.regex.Matcher; 3 4public class RegularExpressionDemo{ 5 public static void main(String[] args){ 6 7 //获取句子中所有长度为3的单词 8 String sentence = "How old are you? I'm 27 years old. what about you? So am I."; 9 String reg = "[a-zA-Z]{3}"; 10 11 Pattern p = Pattern.compile(reg); 12 Matcher m = p.matcher(sentence); 13 14 while(m.find()){ 15 System.out.println(m.group()); 16 } 17 } 18}
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

java基础知识随身记

2018年11月12日20:51:35一、基础知识:1、JVM、JRE和JDK的区别:JVM(JavaVirtualMachine):java虚拟机,用于保证java的跨平台的特性。  java语言是跨平台,jvm不是跨平台的。JRE(JavaRuntimeEnvironment):java的运行环境,包括jvmjava的核心类

java_IO_1

publicclassDirStudy{publicstaticvoidmain(Stringargs){FilefilenewFile("F:/EclipseWorkspace/Java300/temp/test");//mkdir父目录中又不存在

java8 日期类库基本使用

java8日期类库基本使用publicstaticvoidmain(Stringargs){/java比较两个日期的差年月日等/DateTimeFormatterdateTimeFormatter