English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
عبرة فرعية/رمز " \ A يُطابق الكامل للنص في بدايته.
import 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 w3codebox"; Pattern p = Pattern.compile(regex); Matcher m = p.matcher(input); int count = 0; while(m.find()) { count++; } System.out.println("عدد التوافق: " + count); } }
نتيجة الخروج
عدد التوافق: 1
البرنامج التالي باللغة Java يقبل النصوص من المستخدم للتحقق مما إذا كان يحتوي على أحرف غير ASCII.
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class StartingOfInput { public static void main( String args[] ) { String regex = "\\A\\p{ASCII}*\\z"; Scanner sc = new Scanner(System.in); System.out.println("ادخل النص المدخل: "); String input = sc.nextLine(); //إنشاء Pattern object Pattern p = Pattern.compile(regex); //إنشاء Matcher object Matcher m = p.matcher(input); if(m.find()) { System.out.println("المساهمة تحتوي فقط على أحرف ASCII "); } else { System.out.println("المساهمة تحتوي على أحرف غير ASCII "); } } }
ادخل النص المدخل: ما هو اسمك المساهمة تحتوي فقط على أحرف ASCII
ادخل النص المدخل: لماذا نقع في الفشل المساهمة تحتوي على أحرف غير ASCII