java string regex_String 类对正则表达式的支持和java.util.regex开发包

一:JDK1.4之后我们可以直接通过String类来进行正则的调用,String类中有如下方法支持正则的开发:

public boolean matches(String regex):进行字符串验证,匹配某个正则;

public String replaceALL(String regex,String replacement):根据正则的描述替换全部;

public String replaceFirst(String regex,String replacement):根据正则的描述替换首个;

public String[] split(String regex):根据正则进行全部拆分;

public String[] split(String regex,int limit):部分拆分;

下面进行相关示例:

1.字符串的替换:将非字母的字符替换为空

1 public classDemo {2 public static voidmain(String[] args) {3 String str = "gicb*&FAZjncv$#n123hcc";4 String regex = "[^a-zA-Z]";//匹配非字母的字符5 System.out.println(str.replaceAll(regex, ""));6 }7 }

结果为:gicbFAZjncvnhcc

2.字符串的拆分:

1 public classDemo {2 public static voidmain(String[] args) {3 String str = "gicb15bjs6sfc77wws";4 String regex = "\\d+"; //等价与[0-9]+,+表示该正则出现一次或多次

5 String result[] = str.split(regex);//根据正则来拆分成数组

6 for (int x = 0; x < result.length; x++) {7 System.out.println(result[x]);//遍历输出该数组

8 }9 }10 }

结果为:

gicb

bjs

sfc

wws

3.验证某一字符串是否是数字(整数或小数),如果是则将其变成double类型

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 public classDemo {2 public static voidmain(String[] args) {3 String str = "16.8";4 String regex = "\\d+(\\.\\d+)";5 System.out.println(str.matches(regex));//判断

6 if(str.matches(regex)) {7 System.out.println(Double.parseDouble(str));//转换为double类型

8 }9 }10 }

View Code

结果为:

true

16.8

4.判断某一字符串是否是日期或日期时间,如果是则将其变成Date类型

public classDemo {public static void main(String[] args) throwsParseException {

String str= "2018-10-22 10:21:30";

String regex= "\\d{4}-\\d{2}-\\d{2}";

System.out.println(str.matches(regex));//判断是否匹配

if(str.matches(regex)) {

System.out.println(new SimpleDateFormat("yyyy-MM-dd").parse(str));//转换为Date类型

}else if (str.matches("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}")) {

System.out.println(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(str));//转换为Date类型

}

}

}

结果为:

false

Mon Oct 22 10:21:30 GMT+08:00 2018

此时这种需要转换的前提下,对于正则的格式判断最好分开编写(if...elseif...)

5.判断给定的电话号码的格式是否正确(用一个正则进行判断)

电话格式一:52636898【\\d{7,8}】

电话格式一:01052636898【(\\d{3,4})?\\d{7,8}】

电话格式一:(010)-52636898【((\\(\\d{3,4}\\)-)|(\\d{3,4}))?\\d{7,8}】

6.验证给定的字符串是否是Email地址

要求:用户名要求由数字、字母、_组成,其中必须以字母开头,结尾可以有字母和数字,用户名最多不超过15位,最后的根域名只能够是.com.net.cn 、com.cn、  net.cn、 .edu、  .gov、  .org

public classDemo {public static void main(String[] args) throwsParseException {

String str= "Wciu23dhu_ln@ncd2.com";

String regex= "[a-zA-Z][a-zA-Z0-9_\\.]{5,14}[a-zA-Z0-9]@\\w+\\.(net|cn|com|com\\.cn|net\\.cn|org|gov|edu)";

System.out.println(str.matches(regex));//判断是否与正则匹配

}

}

结果为:true

二:java.util.regex开发包

此包中有如下三个类,当需要复杂化正则应用时,用到该开发包。

Pattern 类:

pattern 对象是一个正则表达式的编译表示。Pattern 类没有公共构造方法。要创建一个 Pattern 对象,你必须首先调用其公共静态编译方法,它返回一个 Pattern 对象。该方法接受一个正则表达式作为它的第一个参数。

Matcher 类:

Matcher 对象是对输入字符串进行解释和匹配操作的引擎。与Pattern 类一样,Matcher 也没有公共构造方法。你需要调用 Pattern 对象的 matcher 方法来获得一个 Matcher 对象。

PatternSyntaxException:

PatternSyntaxException 是一个非强制异常类,它表示一个正则表达式模式中的语法错误。

1.Pattern类中的方法:

取得实例化对象:public static Pattern compile(String regex);

拆分字符串:public String[ ] split(CharSequence input);

取得Matcher类对象:public Matcher matcher(CharSequence input);

小例子:通过Pattern类进行字符串拆分

public classDemo {public static void main(String[] args) throwsParseException {

String str= "a|b|c";

String regex= "\\|";

Pattern pattern= Pattern.compile(regex); //取得实例化对象

String []result = pattern.split(str); //拆分结果放在字符串类型的数组里

for(int i=0;i

System.out.println(result[i]);

}

}

}

2.Matcher类负责进行正则匹配:

Matcher类没有提供什么静态方法,通过调用 Pattern 对象的 matcher 方法来获得一个 Matcher 对象,如:

Pattern pattern = Pattern.compile("regexoo");

Matcher matcher = pattern.matcher("string");

此时我们就得到了一个Matcher对象,通过此对象就可以对字符串进行操作了

Matcher类常用的方法:

(1)索引方法(精确表明输入字符串中在哪能找到匹配)

public int start() 返回以前匹配的初始索引

public int end() 返回最后匹配字符之后的偏移量。

(2)研究方法(用来检查输入字符串并返回一个布尔值,表示是否找到该模式)

public boolean lookingAt() 尝试将从区域开头开始的输入序列与该模式匹配。

public boolean find() 尝试查找与该模式匹配的输入序列的下一个子序列。

public boolean matches() 尝试将整个区域与模式匹配。

(3)替换方法(替换输入字符串里文本的方法)

public String replaceAll(String replacement) 替换模式与给定替换字符串相匹配的输入序列的每个子序列。

public String replaceFirst(String replacement) 替换模式与给定替换字符串匹配的输入序列的第一个子序列。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值