Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Python

Python में regex match() और search() Function का महत्व

regex का उपयोग करके दो प्रकार के operations perform किए जा सकते हैं: (a) search और (b) match। Pattern ढूंढने और pattern के साथ match करने के लिए regex efficiently उपयोग करने के लिए, हम इन दो functions का उपयोग कर सकते हैं।

मान लीजिए हमारे पास एक string है। regex match() pattern को केवल string की beginning में check करता है, जबकि regex search() pattern को string में कहीं भी check करता है। match() फंक्शन pattern मिलने पर match object return करता है, अन्यथा none।

  • match() - Pattern को केवल string की beginning में ढूंढता है और matched object return करता है।
  • search() - String में कहीं भी pattern check करता है और matched object return करता है।

इस उदाहरण में, हमारे पास एक string है और हमें इस string में 'engineer' word ढूंढना है।

उदाहरण

import re
pattern = "Engineers"
string = "Scientists dream about doing great things. Engineers Do them"
result = re.match(pattern, string)
if result:
    print("Found")
else:
    print("Not Found")

यह code चलाने पर output print होगा:

आउटपुट

Not Found

अब, आइए उपरोक्त उदाहरण का उपयोग searching के लिए करें:

उदाहरण

import re
pattern = "Engineers"
string = "Scientists dream about doing great things. Engineers Do them"
result = re.search(pattern, string)
if result:
    print("Found")
else:
    print("Not Found")

उपरोक्त code चलाने पर output print होगा:

आउटपुट

Found
  1. - पायथन के re.search और re.match में क्या अंतर है?

    re.match() और re.search() दोनों पायथन मॉड्यूल re के मेथड्स हैं।re.match() मेथड तभी मिलान ढूंढता है जब यह स्ट्रिंग की शुरुआत में होता है। उदाहरण के लिए, स्ट्रिंग TP Tutorials Point TP पर match() कॉल करना और पैटर्न TP खोजना मिलान करेगा।उदाहरणimport re\nresult = re.match(rTP, TP Tutorials Point TP)\npr

  1. - पायथन में search() फ़ंक्शन क्या है?

    पायथन में, search() मॉड्यूल re का एक मेथड है।search() का सिंटैक्स:re.search(pattern, string):यह re.match() के समान है लेकिन यह हमें केवल स्ट्रिंग की शुरुआत में मिलान ढूंढने तक सीमित नहीं करता। re.match() मेथड के विपरीत, यहां स्ट्रिंग TP Tutorials Point TP में पैटर्न Tutorials खोजने पर मिलान लौटाएगा।

  1. - पायथन में match() फ़ंक्शन क्या है?

    पायथन में, match() मॉड्यूल re का एक मेथड है।सिंटैक्सmatch() का सिंटैक्स:re.match(pattern, string):यह मेथड तभी मिलान ढूंढता है जब यह स्ट्रिंग की शुरुआत में होता है। उदाहरण के लिए, स्ट्रिंग TP Tutorials Point TP पर match() कॉल करना और पैटर्न TP खोजना मिलान करेगा। हालांकि, यदि हम केवल Tutorials खोजते ह