java regex

10 0 0
                                        

Java Regex

The Java Regex or Regular Expression is an API to define pattern for searching or manipulating strings.

It is widely used to define constraint on strings such as password and email validation. After learning java regex tutorial, you will be able to test your own regular expressions by the Java Regex Tester Tool.

Java Regex API provides 1 interface and 3 classes in java.util.regex package.

java.util.regex package

It provides following classes and interface for regular expressions. The Matcher and Pattern classes are widely used in java regular expression.

MatchResult interfaceMatcher classPattern classPatternSyntaxException classMatcher class

It implements MatchResult interface. It is a regex engine i.e. used to perform match operations on a character sequence.

No.MethodDescription1boolean matches()test whether the regular expression matches the pattern.2boolean find()finds the next expression that matches the pattern.3boolean find(int start)finds the next expression that matches the pattern from the given start number.4String group()returns the matched subsequence.5int start()returns the starting index of the matched subsequence.6int end()returns the ending index of the matched subsequence.7int groupCount()returns the total number of the matched subsequence.Pattern class

It is the compiled version of a regular expression. It is used to define a pattern for the regex engine.

No.MethodDescription1static Pattern compile(String regex)compiles the given regex and return the instance of pattern.2Matcher matcher(CharSequence input)creates a matcher that matches the given input with pattern.3static boolean matches(String regex, CharSequence input)It works as the combination of compile and matcher methods. It compiles the regular expression and matches the given input with the pattern.4String[] split(CharSequence input)splits the given input string around matches of given pattern. 5String pattern()returns the regex pattern.Example of Java Regular Expressions

There are three ways to write the regex example in java.

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); }} Outputtrue true true Regular Expression . Example

The . (dot) represents a single character.

import java.util.regex.*; class RegexExample2{ public static void main(String args[]){ System.out.println(Pattern.matches(".s", "as"));//true (2nd char is s) System.out.println(Pattern.matches(".s", "mk"));//false (2nd char is not s) System.out.println(Pattern.matches(".s", "mst"));//false (has more than 2 char) System.out.println(Pattern.matches(".s", "amms"));//false (has more than 2 char) System.out.println(Pattern.matches("..s", "mas"));//true (3rd char is s) }} Regex Character classesNo.Character ClassDescription1[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)Regular Expression Character classes Exampleimport java.util.regex.*; class RegexExample3{ public static void main(String args[]){ System.out.println(Pattern.matches("[amn]", "abcd"));//false (not a or m or n) System.out.println(Pattern.matches("[amn]", "a"));//true (among a or m or n) System.out.println(Pattern.matches("[amn]", "ammmna"));//false (m and a comes more than once) }} Regex Quantifiers

CC 104Tempat di mana cerita hidup. Terokai sekarang