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

public static void main(String[] args) {
    if (args.length < 2) {
        System.out.println("Usage:\n" + "java TestRegularExpression " + "characterSequence regularExpression+");
        System.exit(0);//from www.  j  a v  a2s . com
    }
    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[]) {
    // 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   www .  j ava  2s .  c om*/
    System.out.println(matcher.group());
}

From source file:Main.java

License:asdf

public static void main(String[] args) {
    Pattern p = Pattern.compile("\\d+");
    Matcher m = p.matcher("(345+3)*/3asdf234234234/234234234/23asdfaasd1f2a3s4d54fadsf");
    while (m.find())
        System.out.println(m.group());
}

From source file:Resetting.java

public static void main(String[] args) throws Exception {
    Matcher m = Pattern.compile("[frb][aiu][gx]").matcher("fix the rug with bags");
    while (m.find())
        System.out.println(m.group());
    m.reset("fix the rig with rags");
    while (m.find())
        System.out.println(m.group());
}

From source file:PositiveLookaheadExample.java

public static void main(String args[]) {
    String regex = "(?=^255).*";

    Pattern pattern = Pattern.compile(regex);

    String candidate = "255.0.0.1";

    Matcher matcher = pattern.matcher(candidate);

    String ip = "not found";

    if (matcher.find())
        ip = matcher.group();

    String msg = "ip: " + ip;

    System.out.println(msg);//from  w ww  . j av  a 2  s .c om
}

From source file:Main.java

public static void main(String args[]) {
    Pattern pat = Pattern.compile("\\b\\w+@XYZ\\.com\\b");

    Matcher mat = pat.matcher("t@XYZ.com\n" + "a@XYZ.com\n" + "n@XYZ.com");

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

From source file:FindDemo.java

public static void main(String[] args) {
    Matcher m = Pattern.compile("\\w+").matcher("Evening is full of the linnet's wings");
    while (m.find())
        System.out.println(m.group());
    int i = 0;//from  w  w w  .  j  av a  2 s .  c  o m
    while (m.find(i)) {
        System.out.print(m.group() + " ");
        i++;
    }

}

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());
    }/*www .  j a  v  a 2 s. co m*/
}

From source file:Main.java

public static void main(String[] args) {
    // A group of 3 digits followed by 7 digits.
    String regex = "\\b(\\d{3})\\d{7}\\b";

    // Compile the regular expression
    Pattern p = Pattern.compile(regex);

    String source = "12345678, 12345, and 9876543210";

    // Get the Matcher object
    Matcher m = p.matcher(source);

    // Start matching and display the found area codes
    while (m.find()) {
        String phone = m.group();
        String areaCode = m.group(1);
        System.out.println("Phone: " + phone + ", Area  Code:  " + areaCode);
    }//from   w  w w .j  a v  a  2  s  .  com
}

From source file:MainClass.java

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

    String candidateString = "Java Java Java.";

    // Attempt to match the candidate String.
    Matcher matcher = p.matcher(candidateString);

    // loop though and display all matches
    while (matcher.find()) {
        System.out.println(matcher.group());
    }//from  ww w .j  av a2s  . co  m
}