Example usage for java.util.regex Matcher find

List of usage examples for java.util.regex Matcher find

Introduction

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

Prototype

public boolean find() 

Source Link

Document

Attempts to find the next subsequence of the input sequence that matches the pattern.

Usage

From source file:MatcherDemo.java

public static void main(String[] args) {
    Pattern p = Pattern.compile(REGEX);
    Matcher m = p.matcher(INPUT); // get a matcher object
    int count = 0;
    while (m.find()) {
        count++;//w  w  w.j a  v a2s .c o  m
        System.out.println("Match number " + count);
        System.out.println("start(): " + m.start());
        System.out.println("end(): " + m.end());
    }
}

From source file:ArrayCreator.java

public static void main(String... args) {
    Matcher m = p.matcher(s);

    if (m.find()) {
        String cName = m.group(1);
        String[] cVals = m.group(2).split("[\\s,]+");
        int n = cVals.length;

        try {/*from  ww w  .  jav a 2 s. c  o m*/
            Class<?> c = Class.forName(cName);
            Object o = Array.newInstance(c, n);
            for (int i = 0; i < n; i++) {
                String v = cVals[i];
                Constructor ctor = c.getConstructor(String.class);
                Object val = ctor.newInstance(v);
                Array.set(o, i, val);
            }

            Object[] oo = (Object[]) o;
            out.format("%s[] = %s%n", cName, Arrays.toString(oo));

            // production code should handle these exceptions more gracefully
        } catch (ClassNotFoundException x) {
            x.printStackTrace();
        } catch (NoSuchMethodException x) {
            x.printStackTrace();
        } catch (IllegalAccessException x) {
            x.printStackTrace();
        } catch (InstantiationException x) {
            x.printStackTrace();
        } catch (InvocationTargetException x) {
            x.printStackTrace();
        }
    }
}

From source file:Main.java

public static void main(String[] args) {
    String string = "var1[value1], var2[value2], var3[value3]";
    Pattern pattern = Pattern.compile("(\\[)(.*?)(\\])");
    Matcher matcher = pattern.matcher(string);

    List<String> listMatches = new ArrayList<String>();

    while (matcher.find()) {
        listMatches.add(matcher.group(2));
    }//  w w  w  .j  av a2 s  . co  m

    for (String s : listMatches) {
        System.out.println(s);
    }
}

From source file:MatcherTest.java

public static void main(String[] argv) {
    Pattern p = Pattern.compile(REGEX);
    Matcher m = p.matcher(INPUT); // get a matcher object
    int count = 0;
    while (m.find()) {
        count++;/*  w w  w . j  a  v a2 s  .  c om*/
        System.out.println("Match number " + count);
        System.out.println("start(): " + m.start());
        System.out.println("end(): " + m.end());
    }
}

From source file:HrefMatch.java

public static void main(String[] args) {
    try {/*ww  w  . j  a va 2s.c  o  m*/
        // get URL string from command line or use default
        String urlString;
        if (args.length > 0)
            urlString = args[0];
        else
            urlString = "http://java.sun.com";

        // open reader for URL
        InputStreamReader in = new InputStreamReader(new URL(urlString).openStream());

        // read contents into string builder
        StringBuilder input = new StringBuilder();
        int ch;
        while ((ch = in.read()) != -1)
            input.append((char) ch);

        // search for all occurrences of pattern
        String patternString = "<a\\s+href\\s*=\\s*(\"[^\"]*\"|[^\\s>]*)\\s*>";
        Pattern pattern = Pattern.compile(patternString, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(input);

        while (matcher.find()) {
            int start = matcher.start();
            int end = matcher.end();
            String match = input.substring(start, end);
            System.out.println(match);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (PatternSyntaxException e) {
        e.printStackTrace();
    }
}

From source file:MainClass.java

public static void main(String args[]) {

    String phrase = "a word word";
    String duplicatePattern = "\\b(\\w+) \\1\\b";

    Pattern p = null;/*ww w .  j  a va2  s.  c o  m*/
    try {
        p = Pattern.compile(duplicatePattern);
    } catch (PatternSyntaxException pex) {
        pex.printStackTrace();
        System.exit(0);
    }
    // count the number of matches.
    int matches = 0;

    // get the matcher
    Matcher m = p.matcher(phrase);
    String val = null;

    // find all matching Strings
    while (m.find()) {
        val = ":" + m.group() + ":";
        System.out.println(val);
        matches++;
    }
}

From source file:NonCapturingGroupExample.java

public static void main(String args[]) {
    String regex = "hello|hi|greetings|(?:good morning)";

    String candidate1 = "Java2s say hi to you";
    String candidate2 = "Java2s say good morning to you";
    Pattern pattern = Pattern.compile(regex);

    Matcher matcher = pattern.matcher(candidate1);
    System.out.println("GROUP COUNT:" + matcher.groupCount());

    if (matcher.find())
        System.out.println("GOT 1:" + candidate1);

    matcher.reset();/*from   w  ww. ja va 2s . c  o  m*/
    matcher = pattern.matcher(candidate2);

    System.out.println("GROUP COUNT:" + matcher.groupCount());

    if (matcher.find())
        System.out.println("GOT 2:" + candidate2);
}

From source file:RegexTest.java

public static void main(String[] args) {
    Pattern p = Pattern.compile(REGEX);
    Matcher m = p.matcher(INPUT); // get a matcher object
    StringBuffer sb = new StringBuffer();
    while (m.find()) {
        m.appendReplacement(sb, REPLACE);
    }/*from   w ww .ja v a  2 s .c om*/
    m.appendTail(sb);
    System.out.println(sb.toString());
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    CharSequence inputStr = "ab12 cd efg34 123";
    String patternStr = "([a-zA-Z]+[0-9]+)";

    Pattern pattern = Pattern.compile(patternStr);
    Matcher matcher = pattern.matcher(inputStr);

    StringBuffer buf = new StringBuffer();
    boolean found = false;
    while ((found = matcher.find())) {
        String replaceStr = matcher.group();
        matcher.appendReplacement(buf, "found<" + replaceStr + ">");
    }//from   w ww  .ja  va  2 s. co m
    matcher.appendTail(buf);

    String result = buf.toString();
    System.out.println(result);
}

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 ww.  ja va 2s . c o 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");

            }
        }
    }

}