正则表达式——split、match,replaceAll基本用法

正则表达式

串中使用的API涉及正则
split
match
replaceAll

split

要求输出子串,没有空格

public class _正则表达式 {
    public static void main(String[] args) {
        String s = "abc  xyz kkk   ttt";//空格较多时,会产生空串

//     String ss[] = s.split(" {1,}");//" {1,}"正则写法,最少出现1次,最多出现任意多次(不写就是任意多次)
        String ss[] = s.split(" +");//" +"和上面是同一个意思
        //如果用+作分隔符,则必须转义,
        //----- "\\+" 表示 "+"
        //----- "\\{" 表示 "{"
        //----- "\\"  表示 "\"
        for(int i=0;i<ss.length;i++){
            System.out.println(ss[i]);
        }
    }
}

输出结果:

abc
xyz
kkk
ttt

match

匹配是否正确

/*
match
 */
public class _正则表达式02 {
    public static void main(String[] args) {
        String s ="AZ125";
        System.out.println(s.matches("[A-Z]{1,}[0-9]{1,}"));
        //"[A-Z]{1,}[0-9]{1,}" 表示 A到Z出现了1次到无穷次,0到9出现了1次到无穷次
    }
}

/*
可以用于:判断手机号,身份证号,年龄等一些需要用户输入的要素 ,防止用户输入明显错误的信息
[A-Z]{1,5}方括号表示出现的内容,大括号表示出现的次数(量词)

+  表示  {1,}  最少一次

*  表示  {0,}  任意多次

?  表示  {0,1} 零次或一次

 */

输出结果:

true

replaceAll

将2020-12-13转为13/12 2020年

public class 正则表达式03 {
    public static void main(String[] args) {
        String s="abckkd kj 2020-12-13 aa wdqwdasdzdlq";
        s=s.replaceAll("([0-9]{4})-([0-9]{2})-([0-9]{2})","$3/$2 $1年");
        //子组:在正则表达式中,用括号()括起来的部分称为子组
        //$1,表示第一个子组
        //$2,表示第二个子组
        System.out.println(s);
    }

}

输出结果:abckkd kj 13/12 2020年 aa wdqwdasdzdlq

Java正则表达式可用于字符串的匹配、查找、替换和分割等操作。以下是一些基本用法示例: ### 验证字符串是否匹配正则表达式 可以使用 `String` 类的 `matches` 方法来验证一个字符串是否匹配某个正则表达式。例如验证邮箱格式: ```java public class MatchesDemo { public static void main(String[] args) { String email = "xxx123@baidu.com"; //待验证邮箱 String regex = "[a-zA-Z0-9_]+@[a-zA-Z0-9]+(\\.[a-zA-Z]+)+"; //邮箱的正则表达式 //使用regex匹配email是否符合格式要求 boolean match = email.matches(regex); //返回一个boolean值 if(match){ System.out.println("是正确的邮箱格式"); //true }else{ System.out.println("不是正确的邮箱格式"); //false } } } ``` 在上述代码中,定义了一个邮箱字符串 `email` 和对应的邮箱正则表达式 `regex`,通过 `matches` 方法判断 `email` 是否符合 `regex` 的格式要求,并根据结果输出相应信息 [^1]。 ### 查找匹配的子字符串 可以使用 `java.util.regex` 包中的 `Pattern` 和 `Matcher` 类来查找字符串中匹配正则表达式的子字符串。示例如下: ```java import java.util.regex.Matcher; import java.util.regex.Pattern; public class FindMatches { public static void main(String[] args) { String text = "Hello, my email is test@example.com and another is info@test.org"; String regex = "[a-zA-Z0-9_]+@[a-zA-Z0-9]+(\\.[a-zA-Z]+)+"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(text); while (matcher.find()) { System.out.println("找到匹配的邮箱: " + matcher.group()); } } } ``` 在这段代码中,首先使用 `Pattern.compile` 方法编译正则表达式,然后使用 `Matcher` 类的 `find` 方法在字符串中查找匹配的子字符串,并通过 `group` 方法获取匹配的内容。 ### 替换匹配的子字符串 使用 `String` 类的 `replaceAll` 方法可以替换字符串中所有匹配正则表达式的子字符串。示例如下: ```java public class ReplaceMatches { public static void main(String[] args) { String text = "Hello, 123 World!"; String regex = "\\d+"; String replacedText = text.replaceAll(regex, "###"); System.out.println("替换后的字符串: " + replacedText); } } ``` 此代码将字符串 `text` 中所有连续的数字(由 `\\d+` 匹配)替换为 `###`。 ### 分割字符串 使用 `String` 类的 `split` 方法可以根据正则表达式分割字符串。示例如下: ```java public class SplitString { public static void main(String[] args) { String text = "apple,banana;cherry grape"; String regex = "[,;\\s]+"; String[] parts = text.split(regex); for (String part : parts) { System.out.println(part); } } } ``` 上述代码根据逗号、分号和空格(由 `[,;\\s]+` 匹配)将字符串 `text` 分割成多个部分,并输出每个部分。 ### 占有模式 还有一种占有模式,在正则表达式后面加个 `+` 符号就是占有模式,这种模式不会回溯,匹配一旦错了就错了。如果想深入了解,需要了解正则表达式匹配机制 [^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值