DEV Community

Sugumar R
Sugumar R

Posted on

Regex in java

Regex :

regex (short for regular Expression) is a way to search for specific patterns in text.

for example:
1.phone number
2.Email addresses
3.Dates
4.Words starting with "a", ending with "z"...

which package will be:

$ Java Regular Expresion are used through specific class in the java.util.regex packge.

$ These classes use regex patterns they do not extends or inherit them.

Main classes used in java for regex:

1.Pattern - compiles the regex.
2.Matcher - Appies the pattern on a String to find matches.

java developers alredy difine in class background:

pattern and Matcher class:
$ Pattern is a final class.we can not exdends user create class.

Matcher class:
$ Matcher is also a final class- it can not be exdeds in user difine class.

1.Pattern class structure:

Public final class Pattern extends object implements Serializable.

Importans Note:
$ extends object.
$ implement Serializable.

2. Matcher class Structure:

Public final class Matcher extends Object.

important notes:
$ extends Object.

Pattern.compile:

the class is compile() in static method.it is regex String compile,it pattern object chenge .

Image description

Matcher Method use full:#

  1. matches() - full match
  2. find() - finds any match inside String.
  3. group() - returns matched part. 4.start() - start index of match.
  4. end() - End index of match.

Top comments (0)