Example usage for java.util.regex Pattern compile

List of usage examples for java.util.regex Pattern compile

Introduction

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

Prototype

public static Pattern compile(String regex) 

Source Link

Document

Compiles the given regular expression into a pattern.

Usage

From source file:Main.java

public static void main(String[] args) {
    CharSequence inputStr = "quit";
    String patternStr = "q";
    String replacementStr = "s";

    Pattern pattern = Pattern.compile(patternStr);

    Matcher matcher = pattern.matcher(inputStr);
    String output = matcher.replaceAll(replacementStr);
}

From source file:Main.java

public static void main(String[] args) {

    String s = "java2s.com 1 + 1 = 2.0";

    Scanner scanner = new Scanner(s);

    System.out.println(scanner.hasNext(Pattern.compile(".com")));

    System.out.println(scanner.hasNext(Pattern.compile("1")));

    // print the rest of the string
    System.out.println(scanner.nextLine());

    scanner.close();/*from ww  w. ja v  a 2 s.c  o  m*/
}

From source file:Main.java

public static void main(String[] args) {
    CharSequence inputStr = "q w e r t ";
    String patternStr = "q";
    String replacementStr = "s";
    Pattern pattern = Pattern.compile(patternStr);

    Matcher matcher = pattern.matcher(inputStr);
    String output = matcher.replaceAll(replacementStr);
    System.out.println(output);/*from   ww  w .j  av a2s .  c o  m*/
}

From source file:ValidationTestWithPatternAndMatcher.java

public static void main(String args[]) {
    Pattern p = null;//from   w  ww . j ava2 s  .c o m
    try {
        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:Main.java

public static void main(String[] args) {
    Pattern p = Pattern.compile(REGEX);
    Matcher m = p.matcher(INPUT); // get a matcher object
    while (m.find()) {
        System.out.println("start(): " + m.start());
        System.out.println("end(): " + m.end());
    }/*from   ww w  .  j ava 2s.c o  m*/
}

From source file:Main.java

public static void main(String[] args) {
    CharSequence inputStr = "a b c a b c";
    String patternStr = "a";
    String replacementStr = "x";

    Pattern pattern = Pattern.compile(patternStr);

    // Replace all occurrences of pattern in input
    Matcher matcher = pattern.matcher(inputStr);
    String output = matcher.replaceAll(replacementStr);
    System.out.println(output);//from w ww  . jav  a 2  s .c  o  m

}

From source file:MainClass.java

public static void main(String args[]) {

    Pattern pat = Pattern.compile("[ ,.!]");

    String strs[] = pat.split("www.java2s.com java javascript API example.");

    for (int i = 0; i < strs.length; i++)
        System.out.println("Next token: " + strs[i]);

}

From source file:Main.java

public static void main(String[] args) {
    Pattern p = Pattern.compile(REGEX);
    //  get a matcher object
    Matcher m = p.matcher(INPUT);
    List<String> sequences = new Vector<String>();
    while (m.find()) {
        sequences.add(INPUT.substring(m.start(), m.end()));
    }/*from   w  ww.j  av  a 2s. c  o m*/
}

From source file:Main.java

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

    String candidate = "this is a test, A TEST.";

    String regex = "\\ba\\w*\\b";
    Pattern p = Pattern.compile(regex);

    Matcher m = p.matcher(candidate);
    String val = null;
    System.out.println("INPUT: " + candidate);

    System.out.println("REGEX: " + regex + "\r\n");

    while (m.find()) {
        val = m.group();
        System.out.println("MATCH: " + val);
    }/*from   w  w  w  .j  a v  a  2  s  .c  om*/

    if (val == null) {
        System.out.println("NO MATCHES: ");
    }
}

From source file:Main.java

public static void main(String[] args) {
    Scanner scn = new Scanner(System.in);
    String userName;//from   w w w.ja va2  s . c  o  m
    System.out.println("Enter your email address: ");
    userName = scn.findInLine(Pattern.compile("[a-z]+"));

    System.out.println(userName);
}