正则表达式

java.util.regex
正则在线检测 : 非常好用的工具,包括正则的解释以及结果的分析

常见正则表达式

正则表达式可用来搜索、处理字符串

一、Pattern

1、静态方法compile 创建Pattern对象

  public static Pattern compile(String regex) {
        return new Pattern(regex, 0);
    }

2、pattern() 方法返回传入compile中的字符串

3、 静态方法matches 适用于只匹配一次,且匹配全部的字符串

public static boolean matches(String regex, CharSequence input) {
    Pattern p = Pattern.compile(regex);
    Matcher m = p.matcher(input);
    return m.matches();
}

4、Creates a matcher that will match the given input against this pattern.

public Matcher matcher(CharSequence input) {
    Matcher m = new Matcher(this, input);
    return m;
}

二、Matcher

提供分组和多次匹配

1、find() 可以通过while 循环多次执行find方法
Attempts to find the next subsequence of the input sequence that matches the pattern.

public boolean find() {
    synchronized (this) {
        matchFound = findNextImpl(address, matchOffsets);
    }
    return matchFound;
}

2、group 返回匹配到子字符串,配合find使用,matches方法不做任何操作,返回原字符串
Returns the input subsequence matched by the previous match.

public String group() {
    return group(0);
}

m.group() == m.group(0) == 所有匹配的字符(即匹配正则表达式整体结果)

group(1) 列出第一个括号匹配部分,group(2) 列出第二个括号匹配部分,group(3) 列出第三个括号匹配部分。

m.groups() 返回所有括号匹配的字符,以tuple格式。m.groups() == (m.group(0), m.group(1), …)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值