Java 使用正则表达式提取字符串制定的字符

xiaoxiao2021-02-27  186

需求://从字符串中提取指定的字符串,提取字符串的手机号;

String s="ssfsfhshfsfjjs13293016789yfdiyifdsafyasif";

示例代码:

package com.正则表达式; import java.util.regex.Matcher; import java.util.regex.Pattern; public class GetSpecifiedCharacter { public static void main(String[] args) { //从字符串中提取指定的字符串 String s="ssfsfhshfsfjjs13293016789yfdiyifdsafyasif";

//书写正则表达式 String regex="[1][34579]\\d{9}";

//将正则表达式转成正则对象 Pattern pattern =Pattern.compile(regex);

//正则对象与字符串匹配 Matcher matcher=pattern.matcher(s); //匹配成功后打印出找到的结果                                                                      while(matcher.find())

{ System.out.println(matcher.group()); } } }

输出结果为:13293016789

转载请注明原文地址: https://www.6miu.com/read-14475.html

最新回复(0)