Java - Use regex to find digit

Description

Use regex to find digit

Demo

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {

  public static void main(String args[]) {
    String line = "This order was placed for QT3! OK?";
    String pattern = "(.*)(\\d+)(.)";

    // Create a Pattern object
    Pattern r = Pattern.compile(pattern);

    // Now create matcher object.
    Matcher m = r.matcher(line);//from w w  w .  j a  v  a2  s  . c  o  m
    if (m.find()) {
      System.out.println("Found value: " + m.group(0));
      System.out.println("Found value: " + m.group(1));
      System.out.println("Found value: " + m.group(2));
    } else {
      System.out.println("NO MATCH");
    }
  }
}

Related Topic