Example usage for java.util.regex Pattern matcher

List of usage examples for java.util.regex Pattern matcher

Introduction

In this page you can find the example usage for java.util.regex Pattern matcher.

Prototype

public Matcher matcher(CharSequence input) 

Source Link

Document

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

Usage

From source file:MainClass.java

public static void main(String args[]) {
    // create a Pattern
    Pattern p = Pattern.compile("Bond");

    // create a Matcher and use the Matcher.group() method
    String candidateString = "My name is Bond. James Bond.";
    Matcher matcher = p.matcher(candidateString);
    // extract the group
    matcher.find();//from   w w  w. j ava 2 s  .c  o  m
    System.out.println(matcher.group());
}

From source file:com.xue777hua.emails.test.Test.java

public static void main(String[] args) {
    String text = "<div id=\"frag_1\" class=\"page_fragment auth_frag\" data-first=\"true\" data-fid=\"1\"><div class=\"module_topic_paths\"></div><h1 class=\"svTitle\" id=\"tm005\">Effect of inulin and pectin on rheological and thermal properties of potato starch paste and gel</h1><ul class=\"authorGroup noCollab\"><li><a href=\"#\" class=\"authorName\" id=\"authname_N41d730a0N3ee493d4\" data-t=\"a\" data-fn=\"Teresa\" data-ln=\"Witczak\" data-pos=\"1\" data-tb=\"\">Teresa Witczak</a><a title=\"Affiliation: a\" href=\"#af005\" class=\"intra_ref auth_aff\" id=\"baf005\"><sup>a</sup></a><sup>, </sup><a title=\"Corresponding author contact information\" href=\"#cor1\" id=\"bcor1\" class=\"intra_ref auth_corr\"><img class=\"imgLazyJSB\" alt=\"Corresponding author contact information\" src=\"/sd/grey_pxl.gif\" data-inlimg=\"/entities/REcor.gif\"><noscript><img alt=\"Corresponding author contact information\" src=\"http://origin-cdn.els-cdn.com/sd/entities/REcor.gif\"></noscript></a><sup>, </sup><a href=\"mailto:t.witczak@ur.krakow.pl\" class=\"auth_mail\"><img class=\"imgLazyJSB\" src=\"/sd/grey_pxl.gif\" alt=\"E-mail the corresponding author\" data-inlimg=\"/entities/REemail.gif\"><noscript><img src=\"http://origin-cdn.els-cdn.com/sd/entities/REemail.gif\" alt=\"E-mail the corresponding author\"></noscript></a>, </li><li><a href=\"#\" class=\"authorName\" id=\"authname_N41d730a0N3ee4953c\" data-t=\"a\" data-fn=\"Mariusz\" data-ln=\"Witczak\" data-pos=\"2\" data-tb=\"\">Mariusz Witczak</a><a title=\"Affiliation: a\" href=\"#af005\" class=\"intra_ref auth_aff\" id=\"baf005\"><sup>a</sup></a>, </li><li><a href=\"#\" class=\"authorName\" id=\"authname_N41d730a0N3ee495f0\" data-t=\"a\" data-fn=\"Rafa&#322;\" data-ln=\"Ziobro\" data-pos=\"3\" data-tb=\"\">Rafa Ziobro</a><a title=\"Affiliation: b\" href=\"#af010\" class=\"intra_ref auth_aff\" id=\"baf010\"><sup>b</sup></a></li></ul><!--VALIDHTML--><ul class=\"affiliation\"><li id=\"af005\"><sup>a</sup> <span id=\"\">Department of Engineering and Machinery for Food Industry, University of Agriculture in Krakow, Balicka 122 Str., 30-149 Krakow, Poland</span></li><li id=\"af010\"><sup>b</sup> <span id=\"\">Department of Carbohydrates Technology, University of Agriculture in Krakow, Balicka 122 Str., 30-149 Krakow, Poland</span></li></ul><!--VALIDHTML--><!--VALIDHTML--><dl class=\"articleDates\"><dd>Received 24 May 2013, Revised 1 October 2013, Accepted 1 October 2013, Available online 11 October 2013</dd></dl><!--VALIDHTML--><div class=\"moreInformation\"></div><div id=\"ppvPlaceHolder\" class=\"hidden\"></div><!--VALIDHTML--><div id=\"showMoreButtons\"></div><dl class=\"extLinks\"><dd class=\"doiLink\"></dd><dd class=\"rightsLink\"></dd></dl><div class=\"articleOAlabelForced\"></div><div id=\"refersToAndreferredToBy\"><dl id=\"referredToBy\" class=\"documentThread\"><!--Referred To By--></dl></div><!--FRAGMENTEND--><div class=\"page_fragment_ind auth_frag\" data-id=\"frag_2\"></div></div>";
    String authorList = "";
    String articleTitle = "";
    // ???title/*from w w w  .  j a v a2s .  co m*/
    Pattern articleTitlePattern = Pattern.compile("<h1.+?svTitle.+?>(.+?)</h1>");
    Matcher articleTitleMatcher = articleTitlePattern.matcher(text);
    while (articleTitleMatcher.find()) {
        articleTitle = articleTitleMatcher.group(1);
        articleTitle = StringEscapeUtils.unescapeHtml(articleTitle);
        articleTitle = StringUtils.stripTags(articleTitle);
        System.out.println("" + articleTitle);
    }
    // ???
    Pattern p = Pattern.compile("<ul.+?authorGroup.+?>(.+?)</ul>");
    Matcher m = p.matcher(text);
    while (m.find()) {
        authorList = m.group(1);
    }
    p = Pattern.compile("<li>(.+?)</li>");
    m = p.matcher(authorList);
    while (m.find()) {
        String authorItem = m.group(1);
        if (authorItem.contains("mailto")) {
            Pattern nameEmailPattern = Pattern
                    .compile("data-tb=\"[\\d]{0,}\">(.+?)</a>.*href=\"mailto:(.+?)\" class=\"auth_mail\">");
            Matcher nameEmailMatcher = nameEmailPattern.matcher(authorItem);
            if (nameEmailMatcher.find()) {
                String name = nameEmailMatcher.group(1);
                String email = nameEmailMatcher.group(2);
                name = StringEscapeUtils.unescapeHtml(name);
                // ???new ArrayList
                List<String> fieldList = new ArrayList<String>();
                fieldList.add(name);
                fieldList.add(email);
                fieldList.add(articleTitle);
                int hashKey = (name + email + articleTitle).hashCode();
                System.out.println("?????TitlehashKey:" + name + "|" + email
                        + "|" + articleTitle + "|" + hashKey + ",  nameEmailsList");

            }
        }
    }

}

From source file:Main.java

public static void main(String args[]) {
    Pattern p = Pattern.compile("test");
    StringBuffer sb = new StringBuffer();

    String candidateString = "This is a test.";

    String replacement = "Test";
    Matcher matcher = p.matcher(candidateString);
    matcher.find();//from   w ww  .  j a v  a 2  s .c  o m

    matcher.appendReplacement(sb, replacement);
}

From source file:com.mmj.app.common.util.SpiderHtmlUtils.java

public static void main(String[] args) {
    Pattern pattern = Pattern.compile("[http|https]+[://]+[0-9A-Za-z:/[-]_#[?][=][.][&]]*");
    Matcher matcher = pattern.matcher("http://haitao.smzdm.com/youhui/307563");
    System.out.println(matcher.matches());
}

From source file:MainClass.java

public static void main(String[] argv) {
    String pattern = "^q[^u]\\d+\\.";
    String input = "QA777. is the next flight.";

    Pattern reCaseInsens = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);
    Pattern reCaseSens = Pattern.compile(pattern);

    boolean found;
    Matcher m;/*  www  .j a va2  s  .  c  o m*/
    m = reCaseInsens.matcher(input); // will match any case
    found = m.lookingAt(); // will match any case
    System.out.println("IGNORE_CASE match " + found);

    m = reCaseSens.matcher(input); // Get matcher w/o case-insens flag
    found = m.lookingAt(); // will match case-sensitively
    System.out.println("MATCH_NORMAL match was " + found);

}

From source file:MainClass.java

public static void main(String args[]) {
    Pattern p = Pattern.compile("(\\w+) (\\w+)");
    StringBuffer sb = new StringBuffer();

    String candidateString = "Jack Lee";

    String replacement = "$2, $1";
    Matcher matcher = p.matcher(candidateString);
    matcher.matches();//  ww  w . j  a va2s .c  o m

    matcher.appendReplacement(sb, replacement);

    System.out.println(sb.toString());
}

From source file:MainClass.java

public static void main(String[] args) {
    String[] input = new String[] { "Java has regular expressions in 1.4",
            "regular expressions now expressing in Java", "Java represses oracular expressions" };

    Pattern p1 = Pattern.compile("re\\w*"), p2 = Pattern.compile("Java.*");

    for (int i = 0; i < input.length; i++) {
        System.out.println("input " + i + ": " + input[i]);
        Matcher m1 = p1.matcher(input[i]), m2 = p2.matcher(input[i]);
        while (m1.find())
            System.out.println("m1.find() '" + m1.group() + "' start = " + m1.start() + " end = " + m1.end());
    }/*w  w  w  .  j  a  v a2  s.c o  m*/
}

From source file:MainClass.java

public static void main(String args[]) {
    Pattern p = Pattern.compile("J2SE");

    String candidateString_1 = "j2se";
    String candidateString_2 = "J2SE ";
    String candidateString_3 = "J2SE";

    Matcher matcher_1 = p.matcher(candidateString_1);
    Matcher matcher_2 = p.matcher(candidateString_2);
    Matcher matcher_3 = p.matcher(candidateString_3);

    String msg = ":" + candidateString_1 + ": matches?: ";
    System.out.println(msg + matcher_1.matches());

    msg = ":" + candidateString_2 + ": matches?: ";
    System.out.println(msg + matcher_2.matches());

    msg = ":" + candidateString_3 + ": matches?: ";
    System.out.println(msg + matcher_3.matches());
}

From source file:MainClass.java

public static void main(String[] args) {
    String[] input = new String[] { "Java has regular expressions in 1.4",
            "regular expressions now expressing in Java", "Java represses oracular expressions" };

    Pattern p1 = Pattern.compile("re\\w*"), p2 = Pattern.compile("Java.*");

    for (int i = 0; i < input.length; i++) {
        System.out.println("input " + i + ": " + input[i]);
        Matcher m1 = p1.matcher(input[i]), m2 = p2.matcher(input[i]);
        if (m1.lookingAt()) // No reset() necessary
            System.out.println("m1.lookingAt() start = " + m1.start() + " end = " + m1.end());

    }//from   www.j a va 2s .c o m
}

From source file:MatcherMatchesExample.java

public static void main(String args[]) {
    Pattern p = Pattern.compile("J2SE");

    String candidateString_1 = "j2se";
    String candidateString_2 = "J2SE ";
    String candidateString_3 = "J2SE2s";

    Matcher matcher_1 = p.matcher(candidateString_1);
    Matcher matcher_2 = p.matcher(candidateString_2);
    Matcher matcher_3 = p.matcher(candidateString_3);

    String msg = ":" + candidateString_1 + ": matches?: ";
    System.out.println(msg + matcher_1.matches());

    msg = ":" + candidateString_2 + ": matches?: ";
    System.out.println(msg + matcher_2.matches());

    msg = ":" + candidateString_3 + ": matches?: ";
    System.out.println(msg + matcher_3.matches());

}