java 正则表达式Matcher 方法讲解--中篇

  • public boolean lookingAt()

Attempts to match the input sequence, starting at the beginning of the region, against the pattern.

Like the matches method, this method always starts at the beginning of the region; unlike that method, it does not require that the entire region be matched.

If the match succeeds then more information can be obtained via the start, end, and group methods.

lookingAt()方法与matches()方法相似,都是从输入字符序列开始字符开始匹配, 但是不同的的matches()方法需要全局匹配上才能返回true, 而lookingAt()方法只需要从开始匹配到能匹配上就返回true, 如下:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by a123 on 16/12/27.
 */
public class Test {

    public static void main(String[] args) {
        Pattern p = Pattern.compile("java");
        Matcher m = p.matcher("java this ");
        System.out.println(m.lookingAt());//true
        System.out.println(m.matches());//false
    }
}

lookingAt()方法源码:

public boolean lookingAt() {
        return match(from, NOANCHOR);
    }

boolean match(int from, int anchor) {
        this.hitEnd = false;
        this.requireEnd = false;
        from        = from < 0 ? 0 : from;
        this.first  = from;
        this.oldLast = oldLast < 0 ? from : oldLast;
        for (int i = 0; i < groups.length; i++)
            groups[i] = -1;
        acceptMode = anchor;
        boolean result = parentPattern.matchRoot.match(this, from, text);
        if (!result)
            this.first = -1;
        this.oldLast = this.last;
        return result;
    }
  • public static String quoteReplacement(String s)

Returns a literal replacement String for the specified String. This method produces a String that will work as a literal replacement s in the appendReplacement method of the Matcher class. The String produced will match the sequence of characters in s treated as a literal sequence. Slashes (‘\’) and dollar signs (‘$’) will be given no special meaning.

先看源码吧:

public static String quoteReplacement(String s) {
        if ((s.indexOf('\\') == -1) && (s.indexOf('$') == -1))
            return s;
        StringBuilder sb = new StringBuilder();
        for (int i=0; i<s.length(); i++) {
            char c = s.charAt(i);
            if (c == '\\' || c == '$') {
                sb.append('\\');
            }
            sb.append(c);
        }
        return sb.toString();
    }

根据源代码可以知道, 如果s中不包含’\’ 并且 不包含 ‘ s,, ’转换为’$’, 然后返回转换后在字符串


  • public Matcher reset()

Resets this matcher.
Resetting a matcher discards all of its explicit state information and sets its append position to zero. The matcher’s region is set to the default region, which is its entire character sequence. The anchoring and transparency of this matcher’s region boundaries are unaffected.

这个方法是将Matcher对象重置到刚创建的状态

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by a123 on 16/12/27.
 */
public class Test {

    public static void main(String[] args) {
        Pattern p = Pattern.compile("java");
        Matcher m = p.matcher("java this java");

        System.out.println(m.find());//true
        System.out.println(m.find());//true
        System.out.println(m.find());//false
        m = m.reset();
        System.out.println(m.find());//true
    }
}


  • public Matcher region(int start,
    int end)

Sets the limits of this matcher’s region. The region is the part of the input sequence that will be searched to find a match. Invoking this method resets the matcher, and then sets the region to start at the index specified by the start parameter and end at the index specified by the end parameter.

Depending on the transparency and anchoring being used (see useTransparentBounds and useAnchoringBounds), certain constructs such as anchors may behave differently at or around the boundaries of the region.

Parameters:
start - The index to start searching at (inclusive)
end - The index to end searching at (exclusive)

Returns:
this matcher

这个方法和reset()方法相似, 也是重置Matcher对象的状态, 器实现是先将对象归零, 然后将内部的from->start, to -> to

看源码:

  public Matcher region(int start, int end) {
        if ((start < 0) || (start > getTextLength()))
            throw new IndexOutOfBoundsException("start");
        if ((end < 0) || (end > getTextLength()))
            throw new IndexOutOfBoundsException("end");
        if (start > end)
            throw new IndexOutOfBoundsException("start > end");
        reset();
        from = start;
        to = end;
        return this;
    }

样例:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by a123 on 16/12/27.
 */
public class Test {

    public static void main(String[] args) {
        Pattern p = Pattern.compile("java");
        Matcher m = p.matcher("java this java");

        m.region(5, 14);

        System.out.println(m.find());
        System.out.println(m.regionStart());
        System.out.println(m.regionEnd());
        System.out.println(m.find());
        System.out.println(m.regionStart());
        System.out.println(m.regionEnd());

    }
}


  • public String replaceAll(String replacement)


Replaces every subsequence of the input sequence that matches the pattern with the given replacement string.
This method first resets this matcher. It then scans the input sequence looking for matches of the pattern. Characters that are not part of any match are appended directly to the result string; each match is replaced in the result by the replacement string. The replacement string may contain references to captured subsequences as in the appendReplacement method.
Note that backslashes () and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string. Dollar signs may be treated as references to captured subsequences as described above, and backslashes are used to escape literal characters in the replacement string.

Given the regular expression a*b, the input “aabfooaabfooabfoob”, and the replacement string “-“, an invocation of this method on a matcher for that expression would yield the string “-foo-foo-foo-“.

Invoking this method changes this matcher’s state. If the matcher is to be used in further matching operations then it should first be reset.

这个方法是将匹配到的序列全部更改为指定字符串

源码:

public String replaceAll(String replacement) {
        reset();
        boolean result = find();
        if (result) {
            StringBuffer sb = new StringBuffer();
            do {
                appendReplacement(sb, replacement);
                result = find();
            } while (result);
            appendTail(sb);
            return sb.toString();
        }
        return text.toString();
    }

样例:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by a123 on 16/12/27.
 */
public class Test {

    public static void main(String[] args) {
        Pattern p = Pattern.compile("java");
        Matcher m = p.matcher("java this java");

        System.out.println(m.replaceAll("c++"));

    }
}
  • start()/end()这两个方法是返回当前被匹配的子序列的第一个字符位置和最后一个字符序列位置+1
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by a123 on 16/12/27.
 */
public class Test {

    public static void main(String[] args) {
        Pattern p = Pattern.compile("java");
        Matcher m = p.matcher("java this java");
        if (m.find())
            if (m.find()) {
                System.out.println(m.start());//10
                System.out.println(m.end());//14
            }
        System.out.println(m.replaceAll("c++"));

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值