`

正则表达式测试源码

    博客分类:
  • java
阅读更多
正则表达式测试源码
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class RegexTestHarness {

    // Sample
    // Enter your regex: a{3}
    // Enter input string to search: aaaaaaaaa
    // I found the text "aaa" starting at index 0 and ending at index 3.
    // I found the text "aaa" starting at index 3 and ending at index 6.
    // I found the text "aaa" starting at index 6 and ending at index 9.
    public static void main(String[] args) {
        // RegEx 语法
        String regex = "a{3}";
        String check = "aaaaaaaaaa";
        regex = "^R:323.\\d+";
        check = "R:323.234";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(check);

        boolean found = false;
        while (matcher.find()) {
            String msg = String.format("I found the text \"%s\" starting at " + "index %d and ending at index %d.%n", matcher
                    .group(), matcher.start(), matcher.end());
            found = true;
            System.out.println("find【" + found + "】:" + msg);
        }
        if (!found) {
            System.out.println("Not matched !!!");
        }
    }
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics