English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
المحرف “ ^” يتطابق مع بداية النص المحدد، أي أنه يتطابق مع الحرف الأول من النص. على سبيل المثال،
عبرة “ ^ \\ d ”تطابق النصوص التي تبدأ بالرقم. "
عبرة “ ^ [az] ”تطابق النصوص التي تبدأ بحرف صغير. "
إدراج java.util.Scanner; إدراج java.util.regex.Matcher; import java.util.regex.Pattern; public class Example { public static void main(String args[]) { //قراءة النص من المستخدم System.out.println("أدخل String"); Scanner sc = new Scanner(System.in); String input = sc.nextLine(); String regex = "^[^a-zA-Z0-9//s].*"; //ترميم تعبير النص Pattern pattern = Pattern.compile(regex); //استخراج ممثل الرمز Matcher matcher = pattern.matcher(input); if(matcher.matches()) { System.out.println("حدث تطابق"); } else { System.out.println("لم يحدث تطابق"); } } }
نتيجة الخروج
أدخل String #بدءًا بـ رمز خاص حدث التطابق
إدراج java.util.Scanner; إدراج java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample { public static void main( String args[] ) { String regex = \ Scanner sc = new Scanner(System.in); System.out.println("Enter 5 input strings:"); String input[] = new String[5]; for(int i = 0; i < 5; i++) { input[i] = sc.nextLine(); } // create a Pattern object Pattern p = Pattern.compile(regex); for(int i = 0; i < 5; i++) { // create a Matcher object Matcher m = p.matcher(input[i]); if(m.find()) { System.out.println("String " + i + " ينتهي بـ '.'"); } } } }
نتيجة الخروج
Enter 5 input strings: مرحبًا كيف حالك. أين تعيش. ما هو اسمك. مرحبًا بك في w3codebox أكبر مكتبة تعليمية على الإنترنت. String 0 ينتهي بـ '.' String 2 ينتهي بـ '.' String 4 ينتهي بـ '.'
إدراج java.util.Scanner; إدراج java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample { public static void main( String args[] ) { String regex = "^[A-Z]"; Scanner sc = new Scanner(System.in); System.out.println("Enter 5 input strings:"); String input[] = new String[5]; for(int i = 0; i < 5; i++) { input[i] = sc.nextLine(); } // create a Pattern object Pattern p = Pattern.compile(regex); for(int i = 0; i < 5; i++) { // create a Matcher object Matcher m = p.matcher(input[i]); if(m.find()) { System.out.println("String " + i + " starts with a capital letter"); } } } }
نتيجة الخروج
Enter 5 input strings: Sample text1 sample text2 hello how are you Welcome to w3codebox Good morning String 0 starts with a capital letter String 3 starts with a capital letter String 4 starts with a capital letter