Character
class
|
Description
|
[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)
|
[a-d[m-p]]
|
a through d, or m through p: [a-dm-p] (union)
|
[a-z&&[def]]
|
d, e, or f (intersection)
|
[a-z&&[^bc]]
|
a through z, except for b and c
|
[a-z&&[^m-p]]
|
a through z, and not m through p
|
This blog is primarily focus on Java fundamentals and the libraries built on top of Java programming language. Most of the post are example oriented, hope you have fun in reading my blog....:)
Saturday, 1 November 2014
Regular Expression : Character classes
Match any word character \w
\w match a word character : [a-zA-Z_0-9].
For Example, the regular expression '\w+[\s+.]' matches the words in
given string.
Sample Output
Enter Regular Expression
\w+[\s+.]
Enter the string
Hello How are you.
I found the text Hello starting
at index 0 Ending at index 6
I found the text How starting
at index 6 Ending at index 10
I found the text are starting
at index 10 Ending at index 14
I found the text you. starting at index 14 Ending at index 18
Enter the string
I am fine
I found the text I starting at
index 0 Ending at index 2
I found the text am starting at
index 2 Ending at index 5
Note
Match any digit with \d
\d match a digit 0-9.
for Example \d+ match any number of digits
Sample Output
Enter Regular Expression
\d+
Enter the string
0
I found the text 0 starting at index 0 Ending at index 1
Enter the string
12
I found the text 12 starting at index 0 Ending at index 2
Enter the string
123
I found the text 123 starting at index 0 Ending at index 3
Enter the string
Hello 123
I found the text 123 starting at index 6 Ending at index 9
Enter the string
Hello 123 456
I found the text 123 starting at index 6 Ending at index 9
I found the text 456 starting at index 10 Ending at index 13
Note
Use this program to run above sample
Match any Character with .
Metacharacter '.' match any character.
Ex:
Regular expression '.{5}' match any continuous five characters.
Sample Output
Enter Regular Expression
.{5}
Enter the string
Hello How are you
I found the text Hello starting at index 0 Ending at index 5
I found the text How starting at index 5 Ending at index 10
I found the text are y starting at index 10 Ending at index 15
Enter the string
abrakadabra
I found the text abrak starting at index 0 Ending at index 5
I found the text adabr starting at index 5 Ending at index 10
Note
Use this program to
run above sampleRegular Expression : Predefined character classes
The Pattern API contains a number of useful predefined character classes.
Character
class
|
Description
|
.
|
Matches any character
|
\d
|
Matches a digit: [0-9]
|
\D
|
Matches a non-digit: [^0-9]
|
\h
|
Matches a horizontal whitespace character: [
\t\xA0\u1680\u180e\u2000-\u200a\u202f\u205f\u3000]
|
\H
|
Matches a non-horizontal whitespace character:
[^\h]
|
\s
|
Matches a whitespace character: [ \t\n\x0B\f\r]
|
\S
|
Matches a non-whitespace character: [^\s]
|
\v
|
Matches a vertical whitespace character:
[\n\x0B\f\r\x85\u2028\u2029]
|
\V
|
Matches a non-vertical whitespace character:
[^\v]
|
\w
|
Matches a word character: [a-zA-Z_0-9]
|
\W
|
Matches a non-word character: [^\w]
|
Prevoius Next Home
Regular Expression Syntax
The Pattern API contains a number of useful predefined character classes.
Character
class
|
Description
|
.
|
Matches any character
|
\d
|
Matches a digit: [0-9]
|
\D
|
Matches a non-digit: [^0-9]
|
\h
|
Matches a horizontal whitespace character: [
\t\xA0\u1680\u180e\u2000-\u200a\u202f\u205f\u3000]
|
\H
|
Matches a non-horizontal whitespace character:
[^\h]
|
\s
|
Matches a whitespace character: [ \t\n\x0B\f\r]
|
\S
|
Matches a non-whitespace character: [^\s]
|
\v
|
Matches a vertical whitespace character:
[\n\x0B\f\r\x85\u2028\u2029]
|
\V
|
Matches a non-vertical whitespace character:
[^\v]
|
\w
|
Matches a word character: [a-zA-Z_0-9]
|
\W
|
Matches a non-word character: [^\w]
|
Prevoius Next Home
Pattern class
Pattern class provides useful methods to search for a regular expression in given string. To create a pattern, you must first invoke one of its public static compile methods, which will then return a Pattern object. These methods accept a regular expression as the first argument.
import java.util.regex.*; import java.io.*; public class RegExHarness { public static void main(String args[]) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); Pattern pattern; Matcher matcher; String regex; String text; while(true){ try{ System.out.println("Enter Regular Expression"); regex = br.readLine(); pattern = Pattern.compile(regex); System.out.println("Enter the string to search"); text = br.readLine(); matcher = pattern.matcher(text); matcher.matches(); System.out.print("I found the text " + matcher.group()); System.out.print(" starting at index " + matcher.start()); System.out.println(" Ending at index " + matcher.end()); } catch(Exception e){ System.out.println(e); } } } }
Output
Enter Regular Expression a*b Enter the string to search aaaab I found the text aaaab starting at index 0 Ending at index 5 Enter Regular Expression
Prevoius Next Home
Subscribe to:
Posts (Atom)