जावा स्ट्रिंग मिलान () विधि यह जांचती है कि स्ट्रिंग दी गई नियमित अभिव्यक्ति से मेल खाती है या नहीं।
स्ट्रिंग matches()
विधि का सिंटैक्स है:
string.matches(String regex)
यहाँ, स्ट्रिंग String
कक्षा की एक वस्तु है ।
माचिस () पैरामीटर
matches()
विधि एक एकल पैरामीटर लेता है।
- रेगेक्स - एक नियमित अभिव्यक्ति
valueOf () रिटर्न वैल्यू
- यदि रेगेक्स स्ट्रिंग से मेल खाता है, तो सही है
- यदि रेगेक्स स्ट्रिंग से मेल नहीं खाता है तो गलत रिटर्न देता है
उदाहरण 1: जावा मैच ()
class Main ( public static void main(String() args) ( // a regex pattern for // five letter string that starts with 'a' and end with 's' String regex = "^a… s$"; System.out.println("abs".matches(regex)); // false System.out.println("alias".matches(regex)); // true System.out.println("an abacus".matches(regex)); // false System.out.println("abyss".matches(regex)); // true ) )
यहां, "^a… s$"
एक रेगेक्स है, जिसका अर्थ है 5 अक्षर स्ट्रिंग जो एक के साथ शुरू होता है और साथ समाप्त होता है s
।
उदाहरण 2: संख्याओं की जाँच करें
// check whether a string contains only numbers class Main ( public static void main(String() args) ( // a search pattern for only numbers String regex = "^(0-9)+$"; System.out.println("123a".matches(regex)); // false System.out.println("98416".matches(regex)); // true System.out.println("98 41".matches(regex)); // false ) )
यहाँ, "^(0-9)+$"
एक रेगीक्स है, जिसका अर्थ केवल अंक है।
रेगेक्स के बारे में अधिक जानने के लिए, जावा रेगेक्स पर जाएं।