Free Source Code and Tutorials

Custom Search

Regular Expressions

Regular expressions
Java Regex

The Java Regex serves as an API to define pattern for searching strings.
It is used to define constraint on a given strings like password validation or for email validation. After going through this java regex tutorial, you can test your own regular expressions!!
java.util.regex package
It provides classes and interface for making the regular expressions. The Matcher alongwith Pattern classes are widely used in java regular expression.
1. MatchResult interface
2. Matcher class
3. Pattern class
4. PatternSyntaxException class

  1. import java.util.regex.*;
  3. public class Regex{
  4. public static void main(String args[])
  5. {
  6. Pattern p = Pattern.compile(".p");
  7. Matcher m = p.matcher("pen");
  8. boolean c = m.matches();
  9. boolean c2=Pattern.compile(".p").matcher("pen").matches();
  10. boolean c3 = Pattern.matches(".p", "pen");
  11. System.out.println(c+" "+c2+" "+c3);
  12. }}

true true true

Regex Character classes
No. Character Class   Description
1     [abc]               a, b, or c (simple class)
2     [^abc]             Any character except a, b, or c (negation)
3     [a-zA-Z]          a through z or A through Z, inclusive (range)
4     [a-d[m-p]]       a through d, or m through p: [a-dm-p] (union)
5     [a-z&&[def]]    d, e, or f (intersection)
6     [a-z&&[^bc]]    a through z, except for b and c: [ad-z] (subtraction)
7    [a-z&&[^m-p]]   a through z, and not m through p: [a-lq-z](subtraction)

Facebook Comments
(Visited 2 times, 1 visits today)
Share this

Check Your Domain Ranking

Leave a Reply

Your email address will not be published. Required fields are marked * © 2016 Frontier Theme

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!

%d bloggers like this: