找到 2637 篇文章 关于 Java
273 次浏览
子表达式/元字符“\b”在括号外匹配单词边界。在括号内匹配退格键 (0x08)。示例 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample { public static void main( String args[] ) { String regex = "\bbecause\b"; Scanner sc = new Scanner(System.in); System.out.println("Enter a string: "); String input = sc.nextLine(); Pattern p = Pattern.compile(regex); Matcher m = p.matcher(input); int count = 0; while(m.find()) { count ++; ... 阅读更多
320 次浏览
子表达式/元字符“a| b”匹配 a 或 b。示例 1import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample { public static void main( String args[] ) { String regex = "Hello|welcome"; String input = "Hello how are you welcome to Tutorialspoint"; Pattern p = Pattern.compile(regex); Matcher m = p.matcher(input); int count = 0; while(m.find()) { count++; } System.out.println("Number of matches: "+count); } }输出匹配次数:2示例 2以下 Java 程序读取性别 ... 阅读更多
172 次浏览
子表达式/元字符“re{ n, m}”匹配至少 n 次,最多 m 次前面的表达式。示例 1import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample { public static void main( String args[] ) { String regex = "xyy{2, 4}"; String input = "xxyyzxxyyyyxyyzxxyyzz"; Pattern p = Pattern.compile(regex); Matcher m = p.matcher(input); int count = 0; while(m.find()) { count++; } System.out.println("Number of matches: "+count); } }输出匹配次数:1示例 2以下 ... 阅读更多
106 次浏览
子表达式/元字符“re{ n}”精确匹配前面表达式的 n 次出现。示例 1import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample { public static void main( String args[] ) { String regex = "to{1}"; String input = "Welcome to Tutorialspoint"; Pattern p = Pattern.compile(regex); Matcher m = p.matcher(input); int count = 0; while(m.find()) { count++; } System.out.println("Number of matches: "+count); } }输出匹配次数:2示例 2以下 Java 程序读取年龄 ... 阅读更多
108 次浏览
子表达式/元字符“re+”匹配前面表达式的一次或多次出现。示例 1import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample { public static void main( String args[] ) { String regex = "aabc+"; String input = "aabcabcaabcabbcaabcbcaabc"; Pattern p = Pattern.compile(regex); Matcher m = p.matcher(input); int count = 0; while(m.find()) { count++; } System.out.println("Number of matches: "+count); } }输出匹配次数:4示例 2import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Test ... 阅读更多
441 次浏览
子表达式/元字符“\Z”匹配整个字符串的结尾,但不包括允许的最终行终止符。示例 1import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample { public static void main( String args[] ) { String regex = "Tutorialspoint\z"; String input = "Hi how are you welcome to Tutorialspoint"; Pattern p = Pattern.compile(regex); Matcher m = p.matcher(input); int count = 0; while(m.find()) { count++; } System.out.println("Number of matches: "+count); } }输出匹配次数:1示例 ... 阅读更多
116 次浏览
子表达式/元字符“\z”匹配字符串的结尾。示例 1import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample { public static void main( String args[] ) { String regex = "Tutorialspoint\z"; String input = "Hi how are you welcome to Tutorialspoint"; Pattern p = Pattern.compile(regex); Matcher m = p.matcher(input); int count = 0; while(m.find()) { count++; } System.out.println("Number of matches: "+count); } }输出匹配次数:1示例 2以下 Java 程序验证给定的 ... 阅读更多
164 次浏览
子表达式/元字符“\A”匹配整个字符串的开头。示例 1import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample { public static void main( String args[] ) { String regex = "\AHi"; String input = "Hi how are you welcome to Tutorialspoint"; Pattern p = Pattern.compile(regex); Matcher m = p.matcher(input); int count = 0; while(m.find()) { count++; } System.out.println("Number of matches: "+count); } }输出匹配次数:1示例 2以下 Java 程序接受一个 ... 阅读更多
756 次浏览
子表达式/元字符“\G”匹配上次匹配结束的位置。示例import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample { public static void main( String args[] ) { String regex = "\G[0-9]"; Scanner sc = new Scanner(System.in); System.out.println("Enter a string: "); String input = sc.nextLine(); Pattern p = Pattern.compile(regex); Matcher m = p.matcher(input); int count = 0; String digits = ""; System.out.println("Digits in the previous match:"); while(m.find()) { ... 阅读更多
488 次浏览
子表达式/元字符“\D”匹配非数字。示例 1import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample { public static void main( String args[] ) { String regex = "\D"; String input = "This is sample text 12 24 56 89 24"; Pattern p = Pattern.compile(regex); Matcher m = p.matcher(input); int count = 0; while(m.find()) { count++; } System.out.println("Number of matches: "+count); } }输出匹配次数:24示例 2import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public ... 阅读更多