java实战 正则表达式

Last updated on November 20, 2024 am

🧙 Questions

☄️Ideas

基础用法
@Test
public void demo() {

    String text = "某人的名字${张三}";
    Pattern pattern = compile("(\\$\\{).+?}");
    Matcher matcher = pattern.matcher(text);
    if (matcher.find()) {
        System.out.println("username:" + matcher.group().replace("${", "").replace("}", ""));
    }
}
截取链接
@Test
public void demo1() {

    String url = "https://ispong.isxcode.com/path1/path2/path3";
    Pattern pattern = compile("(https://).+?/");
    Matcher matcher = pattern.matcher(url);
    if (matcher.find()) {
        System.out.println("url:" + matcher.group());
    }
}
大小写不敏感
@Test
public void demo2() {

    String text = "A-1-Aa-2-a";
    // 大小写不敏感
    Pattern pattern = compile("a.+?a", Pattern.CASE_INSENSITIVE);
    // 大小写敏感
    // Pattern pattern = compile("a.+?a");
    Matcher matcher = pattern.matcher(text);
    if (matcher.find()) {
        System.out.println("text:" + matcher.group());
    }
}
常用表达式
模板 描述 表达式
{xxx} 特殊括号 \{.+?}
[xxx](xxx) 链接 !\\[.+?\\]\\(.+?\\)
xxx@xx.xx 邮箱 ^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$
^a_* 从头正则 ^a_*

java实战 正则表达式
https://ispong.isxcode.com/spring/java/java 正则表达式/
Author
ispong
Posted on
January 7, 2021
Licensed under