Example usage for java.util.regex Matcher group

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

Introduction

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

Prototype

public String group() 

Source Link

Document

Returns the input subsequence matched by the previous match.

Usage

From source file:ReluctantExample.java

public static void main(String args[]) {
    String regex = "(\\d+?)";
    Pattern pattern = Pattern.compile(regex);

    String candidate = "1234";

    Matcher matcher = pattern.matcher(candidate);

    while (matcher.find()) {
        System.out.println(matcher.group());
    }//from  w  w  w.  java2s .c  o m
    System.out.println("Done");
}

From source file:Main.java

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

    CharSequence inputStr = "a\r\rb"; // Mac
    //inputStr = "a\r\n\r\nb"; // Windows
    //inputStr = "a\n\nb"; // Unix

    String patternStr = "(^.*\\S+.*$)+";
    Pattern pattern = Pattern.compile(patternStr, Pattern.MULTILINE);
    Matcher matcher = pattern.matcher(inputStr);

    while (matcher.find()) {
        String paragraph = matcher.group();
    }/*w  w  w  .j  a v  a2 s .  com*/
}

From source file:Main.java

public static void main(String[] args) {
    String regex = "\\b\\d+\\b";
    StringBuffer sb = new StringBuffer();
    String replacementText = "";
    String matchedText = "";

    String text = "We have 7 tutorials for Java, 2 tutorials for Javascript and 1 tutorial for Oracle.";

    Pattern p = Pattern.compile(regex);
    Matcher m = p.matcher(text);

    while (m.find()) {
        matchedText = m.group();
        int num = Integer.parseInt(matchedText);
        if (num == 1) {
            replacementText = "only one";
        } else if (num < 5) {
            replacementText = "a few";
        } else {/*from  w w  w .  j ava  2s .  c  o  m*/
            replacementText = "many";
        }
        m.appendReplacement(sb, replacementText);
    }

    m.appendTail(sb);
    System.out.println("Old  Text: " + text);
    System.out.println("New Text: " + sb.toString());
}

From source file:Main.java

public static void main(String args[]) {
    String regex = "(\\d+?)";
    Pattern pattern = Pattern.compile(regex);

    String candidate = "1234";

    Matcher matcher = pattern.matcher(candidate);

    while (matcher.find()) {
        System.out.println(matcher.group());
    }/*w  ww.jav a  2s .  c  om*/

    System.out.println("Done");
}

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 information about some of ";
    candidate += "best example codes";

    Matcher matcher = pattern.matcher(candidate);
    while (matcher.find()) {
        String msg = ":" + matcher.group() + ":";
        System.out.println(msg);/*from  w w w  . j  a  v a  2  s. c om*/
    }
}

From source file:MainClass.java

public static void main(String[] args) {
    if (args.length < 2) {
        System.out.println("Usage:\n" + "java MainClass " + "characterSequence regularExpression+");
        System.exit(0);/* www  .  j  a  va 2  s.  co  m*/
    }
    System.out.println("Input: \"" + args[0] + "\"");
    for (int i = 1; i < args.length; i++) {
        System.out.println("Regular expression: \"" + args[i] + "\"");
        Pattern p = Pattern.compile(args[i]);
        Matcher m = p.matcher(args[0]);
        while (m.find()) {
            System.out.println("Match \"" + m.group() + "\" at positions " + m.start() + "-" + (m.end() - 1));
        }
    }
}

From source file:MainClass.java

public static void main(String args[]) {
    // Match lowercase words.
    Pattern pat = Pattern.compile("[a-z]+");
    Matcher mat = pat.matcher("www.java2s.com.");

    while (mat.find())
        System.out.println("Match: " + mat.group());
}

From source file:MainClass.java

public static void main(String[] args) {
    Matcher m = Pattern.compile("\\w+").matcher("Today is Sunday");
    while (m.find())
        System.out.println(m.group());
    int i = 0;// w w w .  ja  v a  2 s.  com
    while (m.find(i)) {
        System.out.print(m.group() + " ");
        i++;
    }
}

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 w  w  .  ja  v a 2s .  c  o m
    }
}

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   w ww.  j  a  v  a 2 s  . c  o m*/
    }
}