English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
عبارات فرعية/رموز " \ b يُناسب الحد الفاصل للكلمات خارج البكارة. يُناسب الفراغات داخل البكارة (0x08).
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 = \\\\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++; } System.out.println("Number of matches: " + count); } }
نتيجة الإخراج
أدخل نصًا: جملة لا تنتهي بـ because لأن because هي وصل عدد التطابق: 3
هذا المثال من جافا يقرأ قيمة النص من المستخدم ويعرض عدد الحدود الكلمية.
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Example { public static void main(String args[]) { System.out.println("Enter input string:"); Scanner sc = new Scanner(System.in); String input = sc.nextLine(); String regex = "\\b"; //编译正则表达式 Pattern pattern = Pattern.compile(regex); //检索匹配器对象 Matcher matcher = pattern.matcher(input); int count = 0; while(matcher.find()) { count++; } System.out.println(count); } }
نتيجة الإخراج
أدخل النص المطلوب: مرحبًا كيف حالك ترحيبًا بيتوكسكوود 14