Java regular expression practice: practical case analysis and application
Introduction:
In Java development, regular expressions are a very powerful and effective tool Tools for pattern matching, replacement, and extraction of strings. It has a wide range of applications and can be used to validate user input, parse and analyze text, data extraction, etc. This article will combine practical cases to deeply explore the use of Java regular expressions and give specific code examples to help readers better understand and apply them in practice.
1. Basic Concepts
Regular expression is a powerful tool for describing string patterns. It consists of characters and special characters, which are used to define matching rules. In Java, regular expressions are supported by the java.util.regex package.
Common regular expression special characters include:
2. Common application cases
import java.util.regex.Pattern; public class EmailValidator { private static final String EMAIL_REGEX = "^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+$"; public static boolean validateEmail(String email) { return Pattern.matches(EMAIL_REGEX, email); } public static void main(String[] args) { String email = "test@example.com"; boolean isValid = validateEmail(email); System.out.println("Is email valid? " + isValid); } }
import java.util.regex.Pattern; public class PhoneValidator { private static final String PHONE_REGEX = "^\+[0-9]{1,3}-[0-9]{10}$"; public static boolean validatePhone(String phone) { return Pattern.matches(PHONE_REGEX, phone); } public static void main(String[] args) { String phone = "+1-1234567890"; boolean isValid = validatePhone(phone); System.out.println("Is phone valid? " + isValid); } }
import java.util.regex.Matcher; import java.util.regex.Pattern; public class UrlExtractor { private static final String URL_REGEX = "(https?://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?)"; public static void extractUrl(String text) { Pattern pattern = Pattern.compile(URL_REGEX); Matcher matcher = pattern.matcher(text); while (matcher.find()) { System.out.println(matcher.group()); } } public static void main(String[] args) { String text = "Visit my website at https://www.example.com for more information."; extractUrl(text); } }
import java.util.regex.Pattern; public class WordCensor { private static final String SENSITIVE_WORD = "(?i)\b(?:bad|evil)\b"; private static final String REPLACEMENT = "****"; public static String censorWords(String text) { Pattern pattern = Pattern.compile(SENSITIVE_WORD); return pattern.matcher(text).replaceAll(REPLACEMENT); } public static void main(String[] args) { String text = "This is a bad example. Don't be evil."; String censoredText = censorWords(text); System.out.println("Censored text: " + censoredText); } }
3. Summary
This article introduces the basic concepts of Java regular expressions and gives common application cases. Through actual code examples, readers can better understand how to use regular expressions to verify email addresses, mobile phone numbers, extract URLs, and replace sensitive words in strings. However, there is still much to explore about the complexity and flexibility of regular expressions. I hope readers can further learn and apply them to unleash their powerful capabilities.
The above is the detailed content of Java regular expressions: practical application cases and detailed explanations. For more information, please follow other related articles on the PHP Chinese website!