Java regular expression syntax detailed explanation and practical guide
Introduction:
Regular expression is a powerful text processing tool, which can be A specific syntax rule to match, find, and replace strings. In the Java programming language, regular expressions can be used through the classes provided by the Java.util.regex package. This article will introduce the syntax of Java regular expressions in detail and provide practical code examples.
1. Basic syntax:
1. Single character matching:
- 字符类:用方括号[]表示,表示从字符列表中匹配一个字符。 例如:[abcd]表示匹配a、b、c、d中的一个字符。 - 范围类:用连字符-表示,表示匹配一个范围内的字符。 例如:[a-z]表示匹配任意小写字母。 - 反向类:用方括号内的^表示,表示匹配除了字符列表中的字符之外的任意字符。 例如:[^a-z]表示匹配除了小写字母之外的任意字符。 - 元字符:用特殊字符表示,有一些特殊字符在正则表达式中有特殊含义。 例如:d表示匹配一个数字字符,s表示匹配任意空白字符。
2. Quantifier matching:
- *:匹配零次或多次。 例如:ab*c可以匹配ac、abc、abbc等。 - +:匹配一次或多次。 例如:ab+c可以匹配abc、abbc等,但不能匹配ac。 - ?:匹配零次或一次。 例如:ab?c可以匹配ac、abc,但不能匹配abbc。 - {n}:匹配恰好n次。 例如:a{3}可以匹配aaa。 - {n,}:匹配至少n次。 例如:a{2,}可以匹配aa、aaa等。 - {n,m}:匹配至少n次,但不超过m次。 例如:a{2,4}可以匹配aa、aaa、aaaa。
3. Boundary matching:
- ^:匹配输入的开始位置。 例如:^abc可以匹配以abc开头的字符串。 - $:匹配输入的结束位置。 例如:abc$可以匹配以abc结尾的字符串。
4. Grouping and capturing:
- (pattern):匹配pattern,并且捕获匹配的内容。 例如:(ab)+可以匹配ab、abab等,并且捕获ab。 - :用于引用分组中捕获的内容。 例如:(w+)s可以匹配两个连续相同的单词。
2. Practical examples:
The following uses several practical code examples to show how to use Java regular expressions.
1. Mobile phone number verification:
public class RegexExample {
public static void main(String[] args) { String regex = "^1[3|4|5|7|8][0-9]{9}$"; String phone1 = "13812345678"; String phone2 = "188123456789"; System.out.println(phone1.matches(regex)); // 输出:true System.out.println(phone2.matches(regex)); // 输出:false }
}
2. Email verification:
public class RegexExample {
public static void main(String[] args) { String regex = "^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$"; String email1 = "test@example.com"; String email2 = "test@com"; System.out.println(email1.matches(regex)); // 输出:true System.out.println(email2.matches(regex)); // 输出:false }
}
3. Extract IP address:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexExample {
public static void main(String[] args) { String regex = "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"; String input = "IP地址是192.168.1.1"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(input); if (matcher.find()) { System.out.println(matcher.group()); // 输出:192.168.1.1 } }
}
Conclusion:
This article details the syntax of Java regular expressions and provides practical code examples. By understanding the syntax and usage examples of regular expressions, readers can flexibly apply regular expressions to solve text processing problems. At the same time, it should be noted that regular expressions may cause performance problems when processing complex patterns, so they need to be carefully evaluated and optimized in actual use. I hope this article will help you understand and apply Java regular expressions.
The above is the detailed content of In-depth analysis and practice of Java regular expression syntax. For more information, please follow other related articles on the PHP Chinese website!