पायथन रेगेक्स
एक रेगेक्स, या रेगुलर एक्सप्रेशन, वर्णों का एक क्रम है जो एक खोज पैटर्न बनाता है।
RegEx का उपयोग यह जांचने के लिए किया जा सकता है कि स्ट्रिंग में निर्दिष्ट खोज पैटर्न है या नहीं।
रेगेक्स मॉड्यूल
पायथन में एक बिल्ट-इन पैकेज होता है re
, जिसे रेगुलर एक्सप्रेशन के साथ काम करने के लिए इस्तेमाल किया जा सकता है।
re
मॉड्यूल आयात करें :
import re
पायथन में रेगेक्स
जब आपने मॉड्यूल आयात कर लिया re
है, तो आप रेगुलर एक्सप्रेशन का उपयोग शुरू कर सकते हैं:
उदाहरण
यह देखने के लिए स्ट्रिंग खोजें कि क्या यह "द" से शुरू होता है और "स्पेन" के साथ समाप्त होता है:
import
re
txt = "The rain in Spain"
x = re.search("^The.*Spain$", txt)
रेगेक्स फ़ंक्शन
मॉड्यूल कार्यों का re
एक सेट प्रदान करता है जो हमें एक मैच के लिए एक स्ट्रिंग खोजने की अनुमति देता है:
Function | Description |
---|---|
findall | Returns a list containing all matches |
search | Returns a Match object if there is a match anywhere in the string |
split | Returns a list where the string has been split at each match |
sub | Replaces one or many matches with a string |
अक्षरों से परे
मेटाकैरेक्टर एक विशेष अर्थ वाले पात्र होते हैं:
Character | Description | Example | Try it |
---|---|---|---|
[] | A set of characters | "[a-m]" | |
\ | Signals a special sequence (can also be used to escape special characters) | "\d" | |
. | Any character (except newline character) | "he..o" | |
^ | Starts with | "^hello" | |
$ | Ends with | "planet$" | |
* | Zero or more occurrences | "he.*o" | |
+ | One or more occurrences | "he.+o" | |
? | Zero or one occurrences | "he.?o" | |
{} | Exactly the specified number of occurrences | "he{2}o" | |
| | Either or | "falls|stays" | |
() | Capture and group |
विशेष क्रम
एक विशेष अनुक्रम \
नीचे दी गई सूची में वर्णों में से एक के बाद होता है, और इसका एक विशेष अर्थ होता है:
Character | Description | Example | Try it |
---|---|---|---|
\A | Returns a match if the specified characters are at the beginning of the string | "\AThe" | |
\b | Returns a match where the specified characters are at the beginning or at the
end of a word (the "r" in the beginning is making sure that the string is being treated as a "raw string") |
r"\bain" r"ain\b" |
|
\B | Returns a match where the specified characters are present, but NOT at the beginning
(or at
the end) of a word (the "r" in the beginning is making sure that the string is being treated as a "raw string") |
r"\Bain" r"ain\B" |
|
\d | Returns a match where the string contains digits (numbers from 0-9) | "\d" | |
\D | Returns a match where the string DOES NOT contain digits | "\D" | |
\s | Returns a match where the string contains a white space character | "\s" | |
\S | Returns a match where the string DOES NOT contain a white space character | "\S" | |
\w | Returns a match where the string contains any word characters (characters from a to Z, digits from 0-9, and the underscore _ character) | "\w" | |
\W | Returns a match where the string DOES NOT contain any word characters | "\W" | |
\Z | Returns a match if the specified characters are at the end of the string | "Spain\Z" |
सेट
एक सेट
[]
एक विशेष अर्थ के साथ वर्ग कोष्ठक की एक जोड़ी के अंदर वर्णों का एक समूह है:
Set | Description | Try it |
---|---|---|
[arn] | Returns a match where one of the specified characters (a ,
r , or n ) are
present |
|
[a-n] | Returns a match for any lower case character, alphabetically between
a and n |
|
[^arn] | Returns a match for any character EXCEPT a ,
r , and n |
|
[0123] | Returns a match where any of the specified digits (0 ,
1 , 2 , or
3 ) are
present |
|
[0-9] | Returns a match for any digit between
0 and 9 |
|
[0-5][0-9] | Returns a match for any two-digit numbers from 00 and
59 |
|
[a-zA-Z] | Returns a match for any character alphabetically between
a and z , lower case OR upper case |
|
[+] | In sets, + , * ,
. , | ,
() , $ ,{}
has no special meaning, so [+] means: return a match for any
+ character in the string |
खोज () फ़ंक्शन
findall()
फ़ंक्शन सभी मैचों वाली एक सूची देता है ।
उदाहरण
सभी मैचों की सूची प्रिंट करें:
import re
txt = "The rain in Spain"
x = re.findall("ai",
txt)
print(x)
सूची में मिलान उस क्रम में होते हैं जिस क्रम में वे पाए जाते हैं।
यदि कोई मिलान नहीं मिलता है, तो एक खाली सूची लौटा दी जाती है:
उदाहरण
यदि कोई मेल नहीं मिला तो एक खाली सूची लौटाएँ:
import re
txt = "The rain in Spain"
x = re.findall("Portugal",
txt)
print(x)
खोज () फ़ंक्शन
search()
फ़ंक्शन एक मैच के लिए स्ट्रिंग की खोज करता है, और यदि कोई मैच है तो मैच ऑब्जेक्ट देता है।
यदि एक से अधिक मैच हैं, तो मैच की केवल पहली घटना वापस की जाएगी:
उदाहरण
स्ट्रिंग में पहले व्हाइट-स्पेस कैरेक्टर की खोज करें:
import re
txt = "The rain in Spain"
x = re.search("\s",
txt)
print("The first white-space character is located in
position:", x.start())
यदि कोई मिलान नहीं मिलता है, तो मान None
वापस कर दिया जाता है:
उदाहरण
ऐसी खोज करें जिससे कोई मेल न मिले:
import re
txt = "The rain in Spain"
x = re.search("Portugal",
txt)
print(x)
विभाजन () फ़ंक्शन
split()
फ़ंक्शन एक सूची देता है जहां प्रत्येक मैच में स्ट्रिंग को विभाजित किया गया है :
उदाहरण
प्रत्येक श्वेत-अंतरिक्ष वर्ण में विभाजित करें:
import re
txt = "The rain in Spain"
x = re.split("\s",
txt)
print(x)
maxsplit
आप पैरामीटर निर्दिष्ट करके घटनाओं की संख्या को नियंत्रित कर सकते हैं
:
उदाहरण
केवल पहली घटना पर स्ट्रिंग को विभाजित करें:
import re
txt = "The rain in Spain"
x = re.split("\s",
txt,
1)
print(x)
उप () फ़ंक्शन
sub()
फ़ंक्शन मैचों को आपकी पसंद के टेक्स्ट से बदल देता है :
उदाहरण
हर व्हाइट-स्पेस कैरेक्टर को 9 नंबर से बदलें:
import re
txt = "The rain in Spain"
x = re.sub("\s",
"9", txt)
print(x)
count
आप पैरामीटर निर्दिष्ट करके प्रतिस्थापन की संख्या को नियंत्रित कर सकते हैं
:
उदाहरण
पहले 2 घटनाओं को बदलें:
import re
txt = "The rain in Spain"
x = re.sub("\s",
"9", txt, 2)
print(x)
वस्तु का मिलान करें
एक मिलान वस्तु एक वस्तु है जिसमें खोज और परिणाम के बारे में जानकारी होती है।
नोट: यदि कोई मिलान नहीं है, None
तो मैच ऑब्जेक्ट के बजाय मान वापस कर दिया जाएगा।
उदाहरण
एक खोज करें जो एक मैच ऑब्जेक्ट लौटाएगा:
import re
txt = "The rain in Spain"
x = re.search("ai",
txt)
print(x) #this will print an object
मैच ऑब्जेक्ट में खोज और परिणाम के बारे में जानकारी प्राप्त करने के लिए उपयोग किए जाने वाले गुण और विधियां हैं:
.span()
एक टपल देता है जिसमें मैच की शुरुआत और अंत की स्थिति होती है।
.string
फ़ंक्शन में पारित स्ट्रिंग लौटाता है, स्ट्रिंग
.group()
का वह भाग लौटाता है जहां एक मिलान था
उदाहरण
पहले मैच की घटना की स्थिति (प्रारंभ- और अंत-स्थिति) प्रिंट करें।
रेगुलर एक्सप्रेशन ऐसे किसी भी शब्द की तलाश करता है जो अपर केस "S" से शुरू होता है:
import re
txt = "The rain in Spain"
x = re.search(r"\bS\w+", txt)
print(x.span())
उदाहरण
फ़ंक्शन में पारित स्ट्रिंग को प्रिंट करें:
import re
txt = "The rain in Spain"
x = re.search(r"\bS\w+", txt)
print(x.string)
उदाहरण
स्ट्रिंग के उस हिस्से को प्रिंट करें जहां एक मैच था।
रेगुलर एक्सप्रेशन ऐसे किसी भी शब्द की तलाश करता है जो अपर केस "S" से शुरू होता है:
import re
txt = "The rain in Spain"
x = re.search(r"\bS\w+", txt)
print(x.group())
नोट: यदि कोई मिलान नहीं है, None
तो मैच ऑब्जेक्ट के बजाय मान वापस कर दिया जाएगा।