In this article, you will learn the use of Regular Expressions in Java Programming Language.
A regular expression may be defined as a collection of characters which are used to search and modify the text and the data by using a pattern matching technique through its specific syntax.
Also Read:- Methods and Constructors in Java Programming Language
The purpose behind incorporating regular expressions in java programming language is to aid java-based applications with the features like email validation, XML document integrity, Searching File and Words etc.
SYNTAX OF REGULAR EXPRESSIONS
The syntax used for regular expressions are similar to that in the Perl Programming Language.
We have shown below some of the regular expressions syntax used in java programming language.
.^
$ \A \z \Z […] [^…] re* re+ re? re{ n} re{n,} re{n,m} \n \B \D \d \S \s \W \w a| b (re) (?: re) | Used to match a single character. Match newline if m option is used.Used to match the beginning of the line.
Used to match the end of the line. Match the beginning of the string. Match the end of the string. Match the end of the string excluding final line terminator. Used to match single character in bracket. Used to match single character outside bracket. Used to match foregoing expression occurring zero or more times. Used to match foregoing expression occurring one or more times. Used to match foregoing expression occurring zero or one time. Match foregoing expression occurring exactly n times. Match foregoing expression occurring n or more times. Match foregoing expression occurring atleast n or atmost m times. Back-reference to capture group number n. Used to match the non-word boundaries. Used to match the non-digits. Used to match the digits. Used to match the non-whitespace. Used to match the whitespace. Used to match the non-word characters. Match the word characters. Used to match a or b. Used for grouping regular expressions along with remembering the matched data. Used for grouping regular expressions without remembering the matched data. |
REGULAR EXPRESSIONS CLASSES
In order to employ pattern matching using regular expressions, java provides java.util.regex package which is mainly comprised of three classes.
- Pattern Class
- Matcher Class
- PatternSyntaxException Class
[1] PATTERN CLASS
(a) Pattern Class Fields
- static int CANON_EQ
- static int CASE_INSENSITIVE
- static int COMMENTS
- static int DOTALL
- static int LITERAL
- static int MULTILINE
- static intUNICODE_CASE
- static int UNICODE_CHARACTER_CLASS
- static int UNIX_LINES
(b) Pattern Class Methods
- static Pattern compile(String regex)
- static Pattern compile(String regex, int flags)
- Predivate<String> asPredicate()
- int flags()
- String pattern()
- static boolean matches(String regex, CharSequence input)
- String[] split(CharSequence input)
- String[] split(CharSequence input, int limit)
- static String quote(String str)
- String toString()
- Stream<String> splitAsStream(CharSequence input)
[2] MATCHER CLASS
Matcher Class Methods
- Matcher appendReplacement(StringBuffer sb, String replacement)
- StringBuffer appendTail(StringBuffer sb)
- int end()
- int end(int group)
- int end(String name)
- String group()
- String group(int group)
- String group(String name)
- int groupCount()
- boolean find()
- boolean find(int start)
- Pattern pattern()
- Matcher region(int start, int end)
- static String quoteReplacement(String str)
- boolean hasAnchoringBounds()
- boolean hasTransparentBounds()
- String replaceAll(String replacement)
- String replaceFirst(String replacement)
- Matcher reset()
- Matcher reset(CharSequence input)
- int start()
- int start(int group)
- int start(String name)
- MatchResult toMatchResult()
- Matcher useAnchoringBounds(boolean bl)
- Matcher usePattern(Pattern newPattern)
- Matcher useTransparentBounds(boolean bl)
- String toString()
[3] PATTERNSYNTAXEXCEPTION CLASS
(a) Constructor
PatternSyntaxException(String desc, String regex, int index)
(b) PatternSyntaxException Class Methods
- String getDescription()
- String getMessage()
- String getPattern()
- int getIndex()
ADVANTAGES OF REGULAR EXPRESSION IN JAVA
- Unicode Support.
- Easy to search, edit or replace the words in a file.
- Email Validation.
- XML document integrity.
- Enable a programmer to remove control characters from a named file.
Also Read: Nested Classes in Java Programming Language
We have provided you the description on the use of Regular Expression in Java Programming Language. Hope you like this article. For more updates and related information, stay connected to our blogs on java programming language.