English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

طريقة Matcher hasAnchoringBounds () في Java مع مثال

java.util.regex.Matcher فئة تمثل محرك تنفيذ جميع العمليات المختلفة في التطابق. هذه الفئة لا تحتوي على مُحرك بناء، يمكنك استخدام طريقة matchs() من فئة java.util.regex.Pattern لإنشاء/الحصول على فئة هذا النوع.

الحدود المثبتة تستخدم لتنسيق النص، مثل ^ و $. بشكل افتراضي، يستخدم الم匹配ر الحدود المثبتة، يمكنك استخدام طريقة useAnchoringBounds() لتغيير الاستخدام من الحدود المثبتة إلى الحدود غير المثبتة.

للفئةhasAnchoringBounds()يحقق هذا (Matcher) ما إذا كان استخدام الحدود المثبتة (إذا كان كذلك)، وإلا يعود صحيحًا، وإلا يعود خطأً.

مثال 1

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class HasAnchoringBoundsExample {
   public static void main(String[] args) {
      String regex = "(.*)(\\d+)(.*)";
      String input = "هذا نص عيني، 1234، يحتوي على أرقام بينهما. ";
         + "\n هذا هو السطر الثاني في النص "
         + "\n هذا هو السطر الثالث في النص";
      //إنشاءُ جسم نموذج
      Pattern pattern = Pattern.compile(regex);
      //Creating a Matcher object
      Matcher matcher = pattern.matcher(input);
      //Verifying for anchoring bounds
      boolean bool = matcher.hasAnchoringBounds();
      //checking for the match
      if(bool) {
         System.out.println("Current matcher uses anchoring bounds");
      }
         System.out.println("Current matcher uses non-anchoring bounds");
      }
      if(matcher.matches()) {
         System.out.println("Match found");
      }
         System.out.println("Match not found");
      }
   }
}

نتیجه خروجی

المطابق الحالي يستخدم حدود التثبيت
لم يتم العثور على التطابق

الامثلة 2

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Trail {
   public static void main( String args[] ) {
      //Reading string value
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter input string");
      String input = sc.nextLine();
      //Regular expression to find digits
      String regex = ".*\\d+.*";
      //Compiling the regular expression
      Pattern pattern = Pattern.compile(regex);
      //Printing the regular expression
      System.out.println("Compiled regular expression: " + pattern.toString());
      //Retrieving the matcher object
      Matcher matcher = pattern.matcher(input);
      matcher.useAnchoringBounds(false);
      boolean hasBounds = matcher.hasAnchoringBounds();
      if(hasBounds) {}}
         System.out.println("Current matcher uses anchoring bounds");
      }
         System.out.println("Current matcher uses non-anchoring bounds");
      }
      //بررسی اینکه آیا تطابق رخ داده است
      if(matcher.matches()) {
         System.out.println("Given String contains digits");
      }
         System.out.println("Given String does not contain digits");
      }
   }
}

نتیجه خروجی

ورودی ورودی را وارد کنید
hello sample 2
عبارت منظم کاربردی: .*\d+.*
مترجم فعلی از محدوده‌های غیرچسبنده استفاده می‌کند
خط داده شامل اعداد
من المحتمل أن تعجبك