Example usage for java.util.regex Pattern CASE_INSENSITIVE

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

Introduction

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

Prototype

int CASE_INSENSITIVE

To view the source code for java.util.regex Pattern CASE_INSENSITIVE.

Click Source Link

Document

Enables case-insensitive matching.

Usage

From source file:Main.java

public static void main(String args[]) {
    Pattern p = Pattern.compile("test", Pattern.CASE_INSENSITIVE);
    String candidateString = "Test.";
    Matcher matcher = p.matcher(candidateString);
    matcher.find(0);/*from w w w  .  ja  v a 2 s. c  o m*/
    System.out.println(matcher.group());

}

From source file:MainClass.java

public static void main(String[] args) {
    Pattern p = Pattern.compile("^java", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
    Matcher m = p.matcher("java has regex\nJava has regex\n" + "JAVA has pretty good regular expressions\n"
            + "Regular expressions are in Java");
    while (m.find())
        System.out.println(m.group());
}

From source file:Main.java

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

    String candidateString = "Java. java JAVA jAVA";

    Matcher matcher = p.matcher(candidateString);

    // display the latter match
    System.out.println(candidateString);
    matcher.find(11);/*from  w  w  w.j  ava 2  s.  c om*/
    System.out.println(matcher.group());

    // display the earlier match
    System.out.println(candidateString);
    matcher.find(0);
    System.out.println(matcher.group());
}

From source file:Main.java

public static void main(String[] args) {
    String str = "this is a test";

    StringBuffer sb = new StringBuffer();
    Matcher m = Pattern.compile("([a-z])([a-z]*)", Pattern.CASE_INSENSITIVE).matcher(str);
    while (m.find()) {
        m.appendReplacement(sb, m.group(1).toUpperCase() + m.group(2).toLowerCase());
    }/*from w w  w.j a va 2 s.c o m*/
    System.out.println(m.appendTail(sb).toString());
}

From source file:Main.java

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

    CharSequence inputStr = "Abc";
    String patternStr = "abc";

    // Compile with case-insensitivity
    Pattern pattern = Pattern.compile(patternStr, Pattern.CASE_INSENSITIVE);
    Matcher matcher = pattern.matcher(inputStr);
    boolean matchFound = matcher.matches();

    matchFound = pattern.matches("[a-c]+", "aBc");
    matchFound = pattern.matches("(?i)[a-c]+", "aBc");

}

From source file:Main.java

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

    CharSequence inputStr = "Abc";
    String patternStr = "abc";

    // Compile with case-insensitivity
    Pattern pattern = Pattern.compile(patternStr, Pattern.CASE_INSENSITIVE);
    Matcher matcher = pattern.matcher(inputStr);
    boolean matchFound = matcher.matches();

    // Use an inline modifier
    matchFound = pattern.matches("abc", "aBc");
    matchFound = pattern.matches("(?i)abc", "aBc");
    matchFound = pattern.matches("a(?i)bc", "aBc");
}

From source file:Main.java

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

    CharSequence inputStr = "Abc";
    String patternStr = "abc";

    // Compile with case-insensitivity
    Pattern pattern = Pattern.compile(patternStr, Pattern.CASE_INSENSITIVE);
    Matcher matcher = pattern.matcher(inputStr);
    boolean matchFound = matcher.matches();

    matchFound = pattern.matches("((?i)a)bc", "aBc");
    matchFound = pattern.matches("(?i:a)bc", "aBc");
    matchFound = pattern.matches("a((?i)b)c", "aBc");
    matchFound = pattern.matches("a(?i:b)c", "aBc");
}

From source file:MainClass.java

public static void main(String[] argv) {
    String pattern = "^q[^u]\\d+\\.";
    String input = "QA777. is the next flight.";

    Pattern reCaseInsens = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);
    Pattern reCaseSens = Pattern.compile(pattern);

    boolean found;
    Matcher m;/*  ww w  .  j  a va  2 s . co  m*/
    m = reCaseInsens.matcher(input); // will match any case
    found = m.lookingAt(); // will match any case
    System.out.println("IGNORE_CASE match " + found);

    m = reCaseSens.matcher(input); // Get matcher w/o case-insens flag
    found = m.lookingAt(); // will match case-sensitively
    System.out.println("MATCH_NORMAL match was " + found);

}

From source file:HrefMatch.java

public static void main(String[] args) {
    try {// ww  w. jav  a2 s .  com
        // get URL string from command line or use default
        String urlString;
        if (args.length > 0)
            urlString = args[0];
        else
            urlString = "http://java.sun.com";

        // open reader for URL
        InputStreamReader in = new InputStreamReader(new URL(urlString).openStream());

        // read contents into string builder
        StringBuilder input = new StringBuilder();
        int ch;
        while ((ch = in.read()) != -1)
            input.append((char) ch);

        // search for all occurrences of pattern
        String patternString = "<a\\s+href\\s*=\\s*(\"[^\"]*\"|[^\\s>]*)\\s*>";
        Pattern pattern = Pattern.compile(patternString, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(input);

        while (matcher.find()) {
            int start = matcher.start();
            int end = matcher.end();
            String match = input.substring(start, end);
            System.out.println(match);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (PatternSyntaxException e) {
        e.printStackTrace();
    }
}

From source file:BGrep.java

public static void main(String[] args) {
    String encodingName = "UTF-8"; // Default to UTF-8 encoding
    int flags = Pattern.MULTILINE; // Default regexp flags

    try { // Fatal exceptions are handled after this try block
        // First, process any options
        int nextarg = 0;
        while (args[nextarg].charAt(0) == '-') {
            String option = args[nextarg++];
            if (option.equals("-e")) {
                encodingName = args[nextarg++];
            } else if (option.equals("-i")) { // case-insensitive matching
                flags |= Pattern.CASE_INSENSITIVE;
            } else if (option.equals("-s")) { // Strict Unicode processing
                flags |= Pattern.UNICODE_CASE; // case-insensitive Unicode
                flags |= Pattern.CANON_EQ; // canonicalize Unicode
            } else {
                System.err.println("Unknown option: " + option);
                usage();/*from  ww  w  . j a  va  2  s  .co m*/
            }
        }

        // Get the Charset for converting bytes to chars
        Charset charset = Charset.forName(encodingName);

        // Next argument must be a regexp. Compile it to a Pattern object
        Pattern pattern = Pattern.compile(args[nextarg++], flags);

        // Require that at least one file is specified
        if (nextarg == args.length)
            usage();

        // Loop through each of the specified filenames
        while (nextarg < args.length) {
            String filename = args[nextarg++];
            CharBuffer chars; // This will hold complete text of the file
            try { // Handle per-file errors locally
                // Open a FileChannel to the named file
                FileInputStream stream = new FileInputStream(filename);
                FileChannel f = stream.getChannel();

                // Memory-map the file into one big ByteBuffer. This is
                // easy but may be somewhat inefficient for short files.
                ByteBuffer bytes = f.map(FileChannel.MapMode.READ_ONLY, 0, f.size());

                // We can close the file once it is is mapped into memory.
                // Closing the stream closes the channel, too.
                stream.close();

                // Decode the entire ByteBuffer into one big CharBuffer
                chars = charset.decode(bytes);
            } catch (IOException e) { // File not found or other problem
                System.err.println(e); // Print error message
                continue; // and move on to the next file
            }

            // This is the basic regexp loop for finding all matches in a
            // CharSequence. Note that CharBuffer implements CharSequence.
            // A Matcher holds state for a given Pattern and text.
            Matcher matcher = pattern.matcher(chars);
            while (matcher.find()) { // While there are more matches
                // Print out details of the match
                System.out.println(filename + ":" + // file name
                        matcher.start() + ": " + // character pos
                        matcher.group()); // matching text
            }
        }
    }
    // These are the things that can go wrong in the code above
    catch (UnsupportedCharsetException e) { // Bad encoding name
        System.err.println("Unknown encoding: " + encodingName);
    } catch (PatternSyntaxException e) { // Bad pattern
        System.err.println("Syntax error in search pattern:\n" + e.getMessage());
    } catch (ArrayIndexOutOfBoundsException e) { // Wrong number of arguments
        usage();
    }
}