Java Regex
The Java Regex or Regular Expression
is an API to define a pattern for
searching or manipulating strings.
It
is widely used to define the constraint on strings such as password and email
validation. After learning Java regex tutorial, you will be able to test your
regular expressions by the Java Regex Tester Tool.Java Regex API provides 1 interface and 3
classes in java.util.regex package.
Fig: Java Regex |
java.util.regex package
The
Matcher and Pattern classes provide the facility of Java regular expression.
The java.util.regex package provides following classes and interfaces for
regular expressions.
1.
MatchResult
interface
2.
Matcher
class
3.
Pattern
class
4.
PatternSyntaxException
class
Example of java Regex
import java.util.regex.*;
public class RegexExample1{
public static void main(String args[]){
//1st way
Pattern p = Pattern.compile(".s");//. represents single character
Matcher m = p.matcher("as");
boolean b = m.matches();
//2nd way
boolean b2=Pattern.compile(".s").matcher("as").matches();
//3rd way
boolean b3 = Pattern.matches(".s", "as");
System.out.println(b+" "+b2+" "+b3);
} }
Output:
true true true
No comments:
Post a Comment