以下是一些Java正则表达式的示例:
String pattern = "\\d+"; String text = "The price is $25.99"; Pattern p = Pattern.compile(pattern); Matcher m = p.matcher(text); while (m.find()) { System.out.println(m.group()); }
输出:25, 99
String pattern = "\\w+@\\w+\\.\\w+"; String text = "My email is john@example.com"; Pattern p = Pattern.compile(pattern); Matcher m = p.matcher(text); if (m.find()) { System.out.println(m.group()); }
输出:john@example.com
String pattern = "\\d{3}-\\d{3}-\\d{4}"; String text = "My phone number is 555-123-4567"; Pattern p = Pattern.compile(pattern); Matcher m = p.matcher(text); if (m.find()) { System.out.println(m.group()); }
输出:555-123-4567
String pattern = "\\d{4}-\\d{2}-\\d{2}"; String text = "The date is 2023-05-09"; Pattern p = Pattern.compile(pattern); Matcher m = p.matcher(text); if (m.find()) { System.out.println(m.group()); }
输出:2023-05-09
这些只是一些简单的示例,正则表达式在Java中的应用非常广泛,可以用于字符串匹配、替换、分割等操作。
String pattern = "^(http|https)://[\\w\\-]+(\\.[\\w\\-]+)+([\\w\\-.,@?^=%&:/~+#]*[\\w\\-@?^=%&/~+#])?$"; String text = "Visit my website at https://www.example.com"; Pattern p = Pattern.compile(pattern); Matcher m = p.matcher(text); if (m.find()) { System.out.println(m.group()); }
输出:https://www.example.com
String pattern = "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])$"; String text = "My IP address is 192.168.1.1"; Pattern p = Pattern.compile(pattern); Matcher m = p.matcher(text); if (m.find()) { System.out.println(m.group()); }
输出:192.168.1.1
String pattern = "[\u4e00-\u9fa5]"; String text = "中文字符"; Pattern p = Pattern.compile(pattern); Matcher m = p.matcher(text); while (m.find()) { System.out.println(m.group()); }
输出:中, 文, 字, 符
String pattern = "\\bcat\\b"; String text = "I have a cat and a caterpillar"; Pattern p = Pattern.compile(pattern); Matcher m = p.matcher(text); String replacedText = m.replaceAll("dog"); System.out.println(replacedText);
输出:I have a dog and a caterpillar
这些示例演示了如何使用Java的正则表达式来匹配不同类型的文本模式。当然,正则表达式可以更加复杂,而这里提供的仅仅是一些入门示例。
String pattern = "<(\"[^\"]*\"|'[^']*'|[^'\">])*>"; String text = "<p>Example HTML code</p>"; Pattern p = Pattern.compile(pattern); Matcher m = p.matcher(text); if (m.find()) { System.out.println(m.group()); }
输出:<p>Example HTML code</p>
<p>Example HTML code</p>
String pattern = "\\s+"; String text = "Multiple spaces between words."; Pattern p = Pattern.compile(pattern); Matcher m = p.matcher(text); String replacedText = m.replaceAll(" "); System.out.println(replacedText);
输出:Multiple spaces between words.
Multiple spaces between words.
String pattern = "\\s+"; String text = "Split this sentence into words."; String[] words = text.split(pattern); for (String word : words) { System.out.println(word); }
输出:Split, this, sentence, into, words.
Split
this
sentence
into
words.
String pattern = "(\\d{4})-(\\d{2})-(\\d{2})"; String text = "The date is 2023-05-09"; Pattern p = Pattern.compile(pattern); Matcher m = p.matcher(text); if (m.find()) { String year = m.group(1); String month = m.group(2); String day = m.group(3); System.out.println("Year: " + year); System.out.println("Month: " + month); System.out.println("Day: " + day); }
输出:Year: 2023, Month: 05, Day: 09
Year: 2023
Month: 05
Day: 09
以上这些示例展示了Java正则表达式的更多应用,包括匹配HTML标签、捕获分组等。正则表达式可以应用于许多不同的场景中,具有很大的灵活性和适应性。
原文链接:codingdict.net