Java 17 Recipes


Download 3.2 Mb.
Pdf ko'rish
bet208/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   204   205   206   207   208   209   210   211   ...   245
Bog'liq
Java 17 Recipes

 Solution 2
When the simple String methods aren’t sufficient, you can use the more powerful
java.util.regex package to work with regular expressions. Create a regular expression 
using the Pattern class. A Matcher works on a String instance using the pattern.
All Matcher operations perform their functions using Pattern and String instances.
The following code demonstrates how to search for ASCII and non-ASCII text in two 
separate strings. See the org.java17recipes.chapter11.recipe11_04.Recipe11_4 class 
for the complete source code. The demoSimple() method finds text with any character 
followed by ".at". The demoComplex() method finds two Japanese symbols in a string.
public void demoSimple() {
Pattern p = Pattern.compile(".at");
Matcher m = p.matcher(enText);
while(m.find()) {
System.out.printf("%s\n", m.group());
}
}
public void demoComplex() {
Pattern p = Pattern.compile("文字");
Matcher m = p.matcher(jaText);
if (m.find()) {
System.out.println(m.group());
}
}
Running these two methods on the previously defined English and Japanese text 
shows the following.
fat
cat
sat
mat
rat
文字
Chapter 11 UniCode, internationalization, and CUrrenCy Codes 


412
 How It Works
The String methods that work with regular expressions are the following.
• public boolean matches(String regex)
• public String replaceFirst(String regex, String 
replacement)
• public String replaceAll(String regex, String replacement)
• public String[] split(String regex, int limit)
• public String[] split(String regex)
The String methods are limited and relatively simple wrappers around the more 
powerful functionality of the java.util.regex classes.
• java.util.regex.Pattern
• java.util.regex.Matcher
• java.util.regex.PatternSyntaxException
The Java regular expressions are similar to those used in the Perl language. Although 
there is a lot to learn about Java regular expressions, the most important points to 
understand from this recipe are the following.
• Your regular expressions can contain non-ASCII characters from the 
full range of Unicode characters.
• Because of a peculiarity of how the Java language compiler 
understands the backslash character, you must use two backslashes 
in your code instead of one for the predefined character class 
expressions.
The most convenient and readable way to use non-ASCII characters in regular 
expressions is to type them directly into your source files using your keyboard input 
methods. Operating systems and editors differ in how they allow you to enter complex 
text outside of ASCII. Regardless of the operating system, you should save the file in the 
UTF-8 encoding if your editor allows it. As an alternate but more difficult way to use 
non-ASCII regular expressions, you can encode characters using the \uXXXX notation. 
Using this notation, instead of directly typing the character using your keyboard, you 
enter 

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   204   205   206   207   208   209   210   211   ...   245




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