Java 17 Recipes


Download 3.2 Mb.
Pdf ko'rish
bet61/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   57   58   59   60   61   62   63   64   ...   245
Bog'liq
Java 17 Recipes

 Solution 2
Use the regular expression Pattern and Matcher classes for a better-performing and 
more versatile matching solution than the matches() method. Although the matches() 
method gets the job done most of the time, there are some occasions in which you 
require a more flexible way of matching. Using this solution is a three-step process.
1. Compile a pattern into a Pattern object.
2. Construct a Matcher object using the matcher() method on the 
Pattern object.
3. Call the matches() method on the Matcher object.
The Pattern object and Matcher object techniques are demonstrated in the 
following example code.
String str = "I love Java 17!";
boolean result = false;
Pattern pattern = Pattern.compile("I love .*[ 0-9]!");
Matcher matcher = pattern.matcher(str);
result = matcher.matches();
System.out.println(result);
The previous example yields a TRUE value, just like its variant, which was 
demonstrated in solution 1.
 How It Works
Regular expressions are a great way to find matches because they allow patterns to be 
defined so that an application does not have to explicitly find an exact string match. They 
can be very useful for finding matches against some text that a user may be typing into 
Chapter 3 StringS


100
your program. However, they could be overkill if you are trying to match strings against 
a string constant you have defined in your program because the String class provides 
many methods that could be used for such tasks. Nevertheless, there certainly comes a 
time in almost every developer’s life when regular expressions can come in handy. They 
can be found in just about every programming language used today. Java makes them 
easy to use and understand.
Note although regular expressions are used in many different languages today, 
the expression syntax for each language varies.
The easiest way to use regular expressions is to call the matches() method on the 
String object. Passing a regular expression to the matches() method yield a Boolean 
result that indicates whether the string matches the given regular expression pattern or 
not. At this point, it is useful to know what a regular expression is and how it works.
regular expression is a string pattern that can be matched against other strings to 
determine its contents. Regular expressions can contain several different patterns that 
enable them to be dynamic in that they can have the ability to match many different 
strings that contain the same format. For instance, in the solution to this recipe, the 
following code can match several different strings.
result = str.matches("I love Java [0-9]!");
The regular expression string in this example is "I love Java [0-9]!", and it 
contains the pattern [0-9], which represents any number between 0 and 9. Therefore, 
any string that reads "I love Java" followed by the numbers 0 through 9 and an 
exclamation point matches the regular expression string. To see a listing of all the 
different patterns used in a regular expression, see the online documentation available at 
the URL in the previous note.
A combination of Pattern and Matcher objects can also achieve similar results 
as the string matcher() method. The Pattern object can compile a string into a 
regular expression pattern. A compiled pattern can provide performance gains to an 
application if the pattern is used multiple times. You can pass the same string-based 
regular expressions to the Pattern.compile() method as you would pass to the string 
matches() method. The result is a compiled Pattern object that can be matched against 
a string for comparison. A Matcher object can be obtained by calling the Pattern object’s 
matcher() method against a given string. Once a Matcher object is obtained, it can 
Chapter 3 StringS


101
match a given string against a pattern using any of the following three methods, which 
each return a Boolean value indicating a match. The following three lines of solution 2 
could be used as an alternate solution to using the Pattern.matches() method, minus 
the reusability of the compiled pattern.
Pattern pattern = Pattern.compile("I love .*[ 0-9]!");
Matcher matcher = pattern.matcher(str);
result = matcher.matches();
• The Matcher object’s matches() method attempts to match the entire 
input string with the pattern.
• The Matcher object’s lookingAt() method attempts to match the 
input string to the pattern starting at the beginning.
• The Matcher object’s find() method scans the input sequence 
looking for the next matching sequence in the string.
In the solution to this recipe, the matches() method is called against the Matcher 
object to match the entire string. Regular expressions can be very useful for matching 
strings against patterns in any event. The technique used for working with the regular 
expressions can vary in different situations, using whichever method works best.

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   57   58   59   60   61   62   63   64   ...   245




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling