JavaRush /Java Blog /Random EN /Regular Expressions in Java. Part 1
articles
Level 15

Regular Expressions in Java. Part 1

Published in the Random EN group
In this tutorial series, we'll learn how to use the java.util.regex API for regular expressions. We will also learn the syntax of regular expressions. Regular Expressions in Java.  Part 1 - 1

What are regular expressions.

Regular expressions are a way to describe a set of strings with common characteristics that each string in the set has. Regular expressions can be used to search and manipulate data. You must learn a special syntax to create regular expressions that goes beyond the normal syntax of the Java language. Regular expressions can be of varying complexity, but once you understand the basics, you can create any kind of regular expression. In these tutorials, we will look at the syntax of regular expressions, as well as sample programs that work with the java.util.regex API , to understand how it all works.

How are regular expressions represented in Java?

The java.util.regex package contains three main classes Pattern, Matcher, and PatternSyntaxException.
  • The object Pattern is a compiled regular expression. Patterndoes not provide public constructors. To create, patternyou need to call public static compilea method that will return an object Pattern. The first argument to this function is a regular expression.

  • The object Matcheris the "engine" that interprets the regular expression and matches it against the input string. Like the class Pattern, Matcherit has no public constructors. You can get an object by calling the object's Matchermethod .matcherPattern

  • PatternSyntaxException- An exception that means a syntax error in an expression.

  • In the following, we will consider each of these classes in detail. But first, you need to understand how regular expressions work. Consider a test program to understand the syntax of regular expressions.

Regular Expressions in Java.  Part 1 - 2Create a program RegexTestHarness.javato learn regular expressions. Command to run the program: java RegexTestHarness, without arguments. The program loop repeats, allowing the user to enter a regular expression and a search string. You don't have to use this program, but it can be useful for testing the examples we'll be looking at.

String literals

The main purpose of templates is to compare strings. For example, if the regular expression fooand the input string are foo, then the program will find a match because the strings are the same. Try this example in our test program.
Enter your regex: foo
Enter input string to search: foo
I found the text foo starting at index 0 and ending at index 3.
Note that the length of the input string is 3, start index 0, end 3: Regular Expressions in Java.  Part 1 - 3Each character of the string is located in its own cell, with indices indicating between cells. The string foo starts at index 0 and ends at 3, even though it occupies cells 0, 1, 2. In the following example, you will notice some peculiarities: the next match starts where the previous one ended: Regular Expressions in Java.  Part 1 - 4

Metacharacters

This API also supports some special characters that define the "behavior" of the regular expression. Let's try to change the input string to catsand the regular expression to cat.. Conclusion:
Enter your regex: cat.
Enter input string to search: cats
I found the text cats starting at index 0 and ending at index 4.
The comparison is still successful even though the dot is in the input string. This happened because the dot is a metacharacter - a special character that has some meaning for matcher'a. The dot metacharacter has the meaning "any character", so the comparison was successful in this example. API Supported Metacharacters: <([{\^-=$!|]})?*+.> There are two ways to mark metacharacters to be treated as regular characters:
  • escape a metacharacter with a backslash
  • enclose at \Q(beginning) and \E(end).
Using this technique, you can put \Qand \Eanywhere in the expression, \Qyou must place it before \E. That's all, in the next lessons we will continue to study regular expressions in Java. Link to original source: Regular expressions in Java. Part 1
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION