Intelligently generate and explain regular expressions, supporting various common pattern matching
Convert your text instructions into formulas or input a formula to have it explained.
Edit Excel online by chatting with AI
Convert your text instructions into SQL queries - powered by AI.
Generate Excel VBA (Visual Basic for Applications) code to automate tasks and create custom solutions within Microsoft Excel.
Upload your Excel file and generate beautiful charts with our AI-powered chart generator.
Convert your text into beautiful mind maps with our AI-powered mind map generator. Edit and customize your mind maps easily.
Use AI to intelligently generate and explain regular expressions, supporting various text pattern matching and data validation.
Java Regular Expressions (Regex) are a powerful tool for text processing, widely used for string matching, validation, replacement, and more. This article provides the most practical Java Regex tutorial, accompanied by detailed code examples, to help you master Java Regex skills and easily handle various development scenarios.
Expression | Description |
---|---|
. |
Matches any character except newline |
^ |
Matches the beginning of the input string |
$ |
Matches the end of the input string |
* |
Matches the previous element 0 or more times |
+ |
Matches the previous element 1 or more times |
? |
Matches the previous element 0 or 1 time |
[] |
Matches any character within the brackets |
[^^] |
Matches any character not within the brackets |
{n} |
Matches exactly n times |
{n,} |
Matches at least n times |
{n,m} |
Matches between n and m times (inclusive) |
\d |
Matches a digit character, equivalent to [0-9] |
\w |
Matches a word character (letter, number, or underscore), equivalent to [a-zA-Z_0-9] |
\s |
Matches any whitespace character |
` | ` |
java.util.regex.Pattern
java.util.regex.Matcher
import java.util.regex.*;
public class RegexExample1 {
public static void main(String[] args) {
String pattern = "^[a-zA-Z0-9]{6,12}$";
String input = "abc12345";
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(input);
if (m.matches()) {
System.out.println("Match successful");
} else {
System.out.println("Match failed");
}
}
}
import java.util.regex.*;
public class RegexExample2 {
public static void main(String[] args) {
String text = "Contact email: [email protected], [email protected]";
String regex = "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-z]{2,}";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(text);
while (m.find()) {
System.out.println("Found email: " + m.group());
}
}
}
import java.util.regex.*;
public class RegexExample3 {
public static void main(String[] args) {
String input = "Customer phone: 13812345678, alternate number: 13987654321";
String regex = "1[3-9]\\d{9}";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);
String result = m.replaceAll("***Confidential***");
System.out.println(result);
}
}
public class RegexValidation {
public static void main(String[] args) {
String phone = "13812345678";
String email = "[email protected]";
String idCard = "110101199003077777";
System.out.println(phone.matches("^1[3-9]\\d{9}$")); // true
System.out.println(email.matches("^[\\w.-]+@[\\w.-]+\\.[a-zA-Z]{2,}$")); // true
System.out.println(idCard.matches("^\\d{17}[\\dXx]$")); // true
}
}
^1[3-9]\\d{9}$
^[\\w.-]+@[\\w.-]+\\.[a-zA-Z]{2,}$
^\\d{17}[\\dXx]$
^(?=.*[A-Za-z])(?=.*\\d)[A-Za-z\\d]{8,}$
^(\\d{1,3}\\.){3}\\d{1,3}$
^https?://[\\w.-]+(?:\\.[\\w\\.-]+)+[/#?]?.*$