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:Main.java

public static void main(final String[] args) {
    final String[] lines = new String[] { "1. New", "2. UK", "3. France" };

    final Pattern p = Pattern.compile("\\.\\s+(.*?)\\s+-\\s+(.*)");

    for (final String unparsedText : lines) {
        String newspaper;/*from   w ww. j ava  2  s.co m*/
        String country;

        final Matcher m = p.matcher(unparsedText);

        if (m.find()) {
            newspaper = m.group(1);
            country = m.group(2);

            System.out.println("Newspaper: " + newspaper + " Country: " + country);
        }
    }
}

From source file:Main.java

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

    String candidateString = "This is another test.";

    String replacement = "$1 AAA $2";
    Matcher matcher = p.matcher(candidateString);
    matcher.find();//from  ww  w  .  j  a  v  a  2s.c  om

    matcher.appendReplacement(sb, replacement);
    String msg = sb.toString();
    System.out.println(msg);
}

From source file:StartEnd.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());
        while (m2.find())
            System.out.println("m2.find() '" + m2.group() + "' start = " + m2.start() + " end = " + m2.end());
        if (m1.lookingAt()) // No reset() necessary
            System.out.println("m1.lookingAt() start = " + m1.start() + " end = " + m1.end());
        if (m2.lookingAt())
            System.out.println("m2.lookingAt() start = " + m2.start() + " end = " + m2.end());
        if (m1.matches()) // No reset() necessary
            System.out.println("m1.matches() start = " + m1.start() + " end = " + m1.end());
        if (m2.matches())
            System.out.println("m2.matches() start = " + m2.start() + " end = " + m2.end());
    }//  w  ww  .  j  a va  2 s.co  m
}

From source file:MainClass.java

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

    String candidateString_1 = "Java is Java";
    String candidateString_2 = "J2SE is Java";
    String candidateString_3 = "J2SEisJava";

    Matcher matcher = p.matcher(candidateString_1);
    String msg = ":" + candidateString_1 + ": matches?: ";
    System.out.println(msg + matcher.lookingAt());

    matcher.reset(candidateString_2);/*from  w  w  w. j av a 2s.c o  m*/
    msg = ":" + candidateString_2 + ": matches?: ";
    System.out.println(msg + matcher.lookingAt());

    matcher.reset(candidateString_3);
    msg = ":" + candidateString_3 + ": matches?: ";
    System.out.println(msg + matcher.lookingAt());

}

From source file:MatcherAppendReplacementGroupExample.java

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

    String candidateString = "My name is Bond. James Bond.";

    String replacement = "$1 Waldo $2";

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

    matcher.appendReplacement(sb, replacement);

    String msg = sb.toString();
    System.out.println(msg);
}

From source file:com.github.liyp.test.TestMain.java

@SuppressWarnings("unchecked")
public static void main(String[] args) {
    // add a shutdown hook to stop the server
    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
        @Override//  www .jav a  2 s. c  o m
        public void run() {
            System.out.println("########### shoutdown begin....");
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("########### shoutdown end....");
        }
    }));

    System.out.println(args.length);
    Iterator<String> iterator1 = IteratorUtils
            .arrayIterator(new String[] { "one", "two", "three", "11", "22", "AB" });
    Iterator<String> iterator2 = IteratorUtils.arrayIterator(new String[] { "a", "b", "c", "33", "ab", "aB" });

    Iterator<String> chainedIter = IteratorUtils.chainedIterator(iterator1, iterator2);

    System.out.println("==================");

    Iterator<String> iter = IteratorUtils.filteredIterator(chainedIter, new Predicate() {
        @Override
        public boolean evaluate(Object arg0) {
            System.out.println("xx:" + arg0.toString());
            String str = (String) arg0;
            return str.matches("([a-z]|[A-Z]){2}");
        }
    });
    while (iter.hasNext()) {
        System.out.println(iter.next());
    }

    System.out.println("===================");

    System.out.println("asas".matches("[a-z]{4}"));

    System.out.println("Y".equals(null));

    System.out.println(String.format("%02d", 1000L));

    System.out.println(ArrayUtils.toString(splitAndTrim(" 11, 21,12 ,", ",")));

    System.out.println(new ArrayList<String>().toString());

    JSONObject json = new JSONObject("{\"keynull\":null}");
    json.put("bool", false);
    json.put("keya", "as");
    json.put("key2", 2212222222222222222L);
    System.out.println(json);
    System.out.println(json.get("keynull").equals(null));

    String a = String.format("{\"id\":%d,\"method\":\"testCrossSync\"," + "\"circle\":%d},\"isEnd\":true", 1,
            1);
    System.out.println(a.getBytes().length);

    System.out.println(new String[] { "a", "b" });

    System.out.println(new JSONArray("[\"aa\",\"\"]"));

    String data = String.format("%9d %s", 1, RandomStringUtils.randomAlphanumeric(10));
    System.out.println(data.getBytes().length);

    System.out.println(ArrayUtils.toString("1|2| 3|  333||| 3".split("\\|")));

    JSONObject j1 = new JSONObject("{\"a\":\"11111\"}");
    JSONObject j2 = new JSONObject(j1.toString());
    j2.put("b", "22222");
    System.out.println(j1 + " | " + j2);

    System.out.println("======================");

    String regex = "\\d+(\\-\\d+){2} \\d+(:\\d+){2}";
    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher("2015-12-28 15:46:14  _NC250_MD:motion de\n");
    String eventDate = matcher.find() ? matcher.group() : "";

    System.out.println(eventDate);
}

From source file:PositiveLookBehindExample.java

public static void main(String args[]) throws Exception {
    String regex = "(?<=http://)\\S+";

    Pattern pattern = Pattern.compile(regex);

    String candidate = "The Java2s website can be found at ";
    candidate += "http://www.java2s.com. There, ";
    candidate += "you can find some Java examples.";

    Matcher matcher = pattern.matcher(candidate);

    while (matcher.find()) {
        String msg = ":" + matcher.group() + ":";
        System.out.println(msg);//from   w  ww .j a  v  a  2 s  .  c o  m
    }
}

From source file:Grep.java

public static void main(String args[]) throws Exception {
    String regex = "";
    InputStream in = System.in;

    regex = args[0];/*  w w w  . j  a v  a 2 s  .co m*/
    in = new BufferedInputStream(new FileInputStream(args[1]));
    Pattern p = null;

    p = Pattern.compile(regex);

    BufferedReader buff = new BufferedReader(new InputStreamReader(in));

    String a;
    for (a = buff.readLine(); a != null; a = buff.readLine()) {
        Matcher m = p.matcher(a);
        if (m.find()) {
            System.out.println(a);
        }
    }
}

From source file:ValidationTestWithPatternAndMatcher.java

public static void main(String args[]) {
    Pattern p = null;
    try {//from  w  w  w.j av a2 s. c  o  m
        p = Pattern.compile("Java \\d");
    } catch (PatternSyntaxException pex) {
        pex.printStackTrace();
        System.exit(0);
    }

    String candidate = "I love Java 5";
    Matcher m = p.matcher(candidate);

    System.out.println("result=" + m.find());
}

From source file:PositiveLookBehindExample.java

public static void main(String args[]) throws Exception {
    String regex = "(?<=http://)\\S+";

    Pattern pattern = Pattern.compile(regex);

    String candidate = "The Java2s website can be found at ";
    candidate += "http://www.java2s.com. There, ";
    candidate += "you can find some best example code for Java.";

    Matcher matcher = pattern.matcher(candidate);

    while (matcher.find()) {
        String msg = ":" + matcher.group() + ":";
        System.out.println(msg);//from www  .ja v  a  2  s. c  o  m
    }
}