Home >Java >javaTutorial >How to use Regex function in Java for regular expression matching
Regular expression is an expression for string operations, which can be used in various scenarios such as searching, replacing, and validating strings. In Java, you can use the Regex function for regular expression matching operations. This article will introduce how to use the Regex function in Java for regular expression matching.
1. Introduction to Regex function
The Regex function in Java belongs to the java.util.regex package and provides a variety of methods to perform regular expression matching operations. The core classes are Pattern and Matcher.
The Pattern class is used to define the pattern of regular expressions. Its constructor is Pattern.compile(String regex), where regex is the string representation of the regular expression.
The Matcher class is used to match strings. Its constructor is pattern.matcher(String input), where pattern is the Pattern object and input is the string that needs to be matched.
2. The syntax of regular expressions
Before matching regular expressions, you need to understand the syntax of regular expressions. The following are some basic elements of regular expression syntax:
3. Use of Regex function
The general process of using the Regex function is: define the pattern of the regular expression (i.e. Pattern object), and perform matching operations on the strings that need to be matched. (i.e. Matcher object), query the matching results.
The following is a basic usage example of the Regex function:
import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample { public static void main(String[] args) { String regex = "cat"; String input1 = "The cat is on the mat."; String input2 = "A cat and a dog."; Pattern pattern = Pattern.compile(regex); Matcher matcher1 = pattern.matcher(input1); Matcher matcher2 = pattern.matcher(input2); System.out.println("input1中是否包含cat:" + matcher1.find()); System.out.println("input2中是否包含cat:" + matcher2.find()); } }
The output result is:
input1中是否包含cat:true input2中是否包含cat:true
In the above code, a regular expression pattern is first defined, that is, the string " cat". Then perform matching operations on two different input strings.
In the specific matching operation, first create a Pattern object through the Pattern.compile(String regex) method, and then create a Matcher object through the matcher(String input) method of the object, and then you can use the Matcher object The find() method queries whether the input string contains a sequence of characters in the pattern.
In addition to using the find() method, the Matcher object also provides many other methods for obtaining different information about the matching results, such as the starting position of the match, the matched substring, etc.
4. Commonly used regular expression examples
public static boolean isDigit(String str) { Pattern pattern = Pattern.compile("[0-9]*"); return pattern.matcher(str).matches(); }
public static boolean isEmail(String str) { Pattern pattern = Pattern.compile("^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"); return pattern.matcher(str).matches(); }
public static boolean isMobileNumber(String str) { Pattern pattern = Pattern.compile("^1\d{10}$"); return pattern.matcher(str).matches(); }
public static boolean isIDCardNumber(String str) { Pattern pattern = Pattern.compile("^\d{17}[0-9xX]$"); return pattern.matcher(str).matches(); }
5. Notes
The above is the introduction and examples of using the Regex function in Java for regular expression matching. I hope it will be helpful to you!
The above is the detailed content of How to use Regex function in Java for regular expression matching. For more information, please follow other related articles on the PHP Chinese website!