Regex Constructs
In general regular expression include characters, character classes and quantifiers. The table below provides a reference to most common Regex constructs taken from the Java reference material.
Construct
|
Matches
|
---|---|
Characters | |
x | The character x |
\\ | The backslash character |
\t | The tab character |
\n | The newline character |
\r | The carriage-return character |
Character Classes | |
[abc] | a, b, or c (simple class) |
[^abc] | Any character except a, b, or c (negation) |
[a-zA-Z] | a through z or A through Z, inclusive (range) |
Predefined character classes
|
|
. | Any character (may or may not match line terminators) |
\d | A digit: [0-9] |
\s | A whitespace character: [ \t\n\x0B\f\r] |
\S | A non-whitespace character: [^\s] |
\w | A word character: [a-zA-Z_0-9] |
\W | A non-word character: [^\w] |
Boundary Matchers | |
^ | The beginning of a line |
$ | The end of a line |
\b | A word boundary |
\B | A non-word boundary |
Quantifiers | |
X? | X, once or not at all |
X* | X, zero or more times |
X+ | X, one or more times |
X{n} | X, exactly n times |
X{n,} | X, at least n times |
X{n,m} | X, at least n but not more than m times |
Regex Java APIs
Java supports java.util.regex for regular expressions processing.
- Primarily there are 2 classes Pattern and Matcher.
- Pattern is used to define a regular expression. Use the Pattern.compile() factory method to create a pattern.
- Matcher is used to match the pattern against a input sequence. Use Pattern.matcher() to get create a Matcher.
package com.sourcetricks.regex; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegExTest { public static void main(String[] args) { // Matches the entire region String str = "java"; Pattern pattern = Pattern.compile("java"); Matcher matcher = pattern.matcher(str); if ( matcher.matches() ) { System.out.println("Pattern found."); } // Find if a pattern exists str = "This is a java tutorial"; pattern = Pattern.compile("java"); matcher = pattern.matcher(str); if ( matcher.find() ){ System.out.println("Pattern found at location = " + matcher.start()); } // Using flags to perform case insensitive match str = "This is a Java tutorial"; pattern = Pattern.compile("java", Pattern.CASE_INSENSITIVE); matcher = pattern.matcher(str); if ( matcher.find() ){ System.out.println("Pattern found at location = " + matcher.start()); } // Split a string using a whitespace str = "This is a Java tutorial"; pattern = Pattern.compile("\\s"); String[] words = pattern.split(str); for ( String word : words ) System.out.println(word); // Find all occurrences of a pattern str = "This is a Java tutorial. Java is programming language."; pattern = Pattern.compile("[jJ]ava"); matcher = pattern.matcher(str); while ( matcher.find() ) { System.out.println("Pattern found at location = " + matcher.start()); } // Replace all occurrences of a pattern String str1 = matcher.replaceAll("C++"); System.out.println(str1); } }This example produces the following output.
Pattern found. Pattern found at location = 10 Pattern found at location = 10 This is a Java tutorial Pattern found at location = 10 Pattern found at location = 25 This is a C++ tutorial. C++ is programming language.
0 comments:
Post a Comment