Python में regular expression ये string पर operation करता है |
String पर match या किसी string को find/search करने के लिए regular expression काफी मददगार साबित होता है |
Python में अगर regular expression का इस्तेमाल करना हो तो 're' इस module का इस्तेमाल करना पड़ता है |
किसी भी regular expression के हिस्से का इस्तेमाल करना हो तो program पर 're' इस module को import करना पड़ता है |
जैसे कि.
import re
Raw String Notation for Regular Expression Patterns
Python में जब regular expression लिखना होता है तब Normal string के बजाय raw string का इस्तेमाल किया जाता है |
Raw String की शुरुआत 'r' इस prefix से की जाती है | अगर string पर 'r'(raw string) prefix दिया जाता है तो backslashes(\\) को किसी भी प्रकार से handle नहीं किया जा सकता है |
Difference Between Normal String and Raw String
Normal String
Normal String में '\n'(escape sequence) ये एक ही character होता है |
print(len("\n")) #return 1 print("Hello World\nHello World") #return #Hello World #Hello World
Raw String
Raw String में '\' और 'n' ये दोनों अलग-अलग characters होते है |
print(len(r"\n")) #return 2 print("Hello World\nHello World") #return Hello World\nHello World
In Regular Expression
Regular Expression में raw string का इस्तेमाल pattern के रूप में काफी बार किया जाता है |
"\\d+\\w" #Normal String r"\d+\w" #Raw String
Matching a String
String से match करने के लिए re module के 'match()' function का इस्तेमाल किया जाता है |
Syntax for match() Regular Expression Function
re.match(pattern, raw/string, flags)
Parameters
pattern : यहाँ पर pattern दिया जाता है |
raw/string : यहाँ पर pattern से match करने के लिए string या raw string दी जाती है |
flags : Optional. यहाँ पर एक या एक से ज्यादा flags दिए जाते है | Bitwise OR(|) से एक से ज्यादा flags का इस्तेमाल किया जाता है |
Example for match() Function in Python
Example में regular expression के match() function का इस्तेमाल किया गया है |
pattern : r"(\w+)\ (\d+)\ (\w+)
ये raw string दी गयी है | पहले group में characters(\w+) को match किया गया है बाद में एक space(\ ) दिया गया है उसके बाद digits(\d+) को match किया गया है उसके बाद फिर एक space(\ ) और आखिर में फिर characters को match किया गया है |
str : Hello 123 Hello
इस normal string पर operation किया गया है |
Output :import re str = "Hello 123 Hello" a = re.match(r"(\w+)\ (\d+)\ (\w+)", str) print("group() :",a.group()) #return entire matches ((\w+)\ (\d+)\ (\w+)) print("group(0) :",a.group(0)) #return entire matches ((\w+)\ (\d+)\ (\w+)) print("group(1) :",a.group(1)) #return first subgroup match (\w+) print("group(2) :",a.group(2)) #return seconnd subgroup match (\d+) print("group(3) :",a.group(3)) #return third subgroup match (\w+)
group() : Hello 123 Hello group(0) : Hello 123 Hello group(1) : Hello group(2) : 123 group(3) : Hello
Python में Regular Expression में String पर operation करने के लिए कुछ हिस्से बनाये गए है |
- Character Classes
- Quantifiers
- Metacharacter
- Modifiers/Flags
- Regular Expression Functions
- Regular Expression Object Methods
- Regular Expression Match Object Methods
1. Character Classes for Regular Expression in Python
Expressions | Description |
---|---|
[..] | कोई भी character या digits ढूंढने के लिए दिया जाता है | |
[^..] | कोई भी character या digits दिया जाता है , उसे ढूंढा नहीं जाता | |
[0-9] | 0 से 9 तक के digits को ढूंढा जाता है | |
[^0-9] | 0 से 9 तक के digits को ढूंढा नहीं जाता है | |
[A-Z] | Uppercase A से लेकर uppercase Z तक characters को ढूंढा जाता है | |
[^A-Z] | Uppercase A से लेकर uppercase Z तक characters को ढूंढा नहीं जाता है | |
[a-z] | lowercase a से लेकर lowercase z तक characters को ढूंढा जाता है | |
[^a-z] | lowercase a से लेकर lowercase z तक characters को ढूंढा नहीं जाता है | |
[mf]ail | यहाँ पर mail और fail को string पर ढूंढा जाता है | |
Grac[ey] | यहाँ पर Grace और Gracy को string पर ढूंढा जाता है | |
2. Quantifiers for Regular Expression in Python
Expressions | Description |
---|---|
n+ | String में कम से कम एक या उससे ज्यादा 'n' occurrences को match किया जाता है | |
n* | String में zero या उससे ज्यादा 'n' occurrences से match किया जाता है | |
n? | String में zero या एक occurrence 'n' से match किया जाता है | |
n{X} | String में से n को sequences of X number तक match किया जाता है | |
n{X, Y} | String में से n को sequences of X number से Y number तक match किया जाता है | |
n{X,} | String में से n को sequences of कम से कम X number तक match किया जाता है | |
n$ | String में से n को end पर match किया जाता है | अगर multiline(re.M) mode होता है तो हर newline के end पर match किया जाता है | |
^n | String में से n को start पर match किया जाता है | अगर multiline(re.M) mode होता है तो हर newline के start पर match किया जाता है | |
p|q | String में से p या q को match किया जाता है | |
python+ | String में से python या python में 'n' एक या एक से ज्यादा match किया जाता है | |
python* | String में से python या python में 'n' शून्य या उससे ज्यादा match किया जाता है | |
python? | String में से python या python में 'n' शून्य या एक match किया जाता है | |
3. Metacharacters for Regular Expression in Python
Expressions | Description |
---|---|
dot(.) | single dot से single character ढूंढा जाता है | |
b | String में से word के शुरुआत या end के matches ढूंढे जाते है | |
B | String में से word के शुरुआत या end के matches ढूंढे नहीं जाते है | |
d | String में से 0 से 9 numbers तक ढूंढा जाता है | |
D | String में से non-numbers character ढूंढा जाता है | |
f | String में से form-feed character को ढूंढा जाता है | |
n | String में से newline character को ढूंढा जाता है | |
r | String में से carriage return character को ढूंढा जाता है | |
s | String में से whitespace character को ढूंढा जाता है | |
S | String में से non-whitespace character को ढूंढा जाता है | |
t | String में से tab character को ढूंढा जाता है | |
v | String में से vertical tab character को ढूंढा जाता है | |
w | String में से word character को ढूंढा जाता है | |
W | String में से non-word character को ढूंढा जाता है | |
0 | String में से null character को ढूंढा जाता है | |
4. Modifiers/Flags for Regular Expression in Python
Function या Method पर एक से ज्यादा Modifiers/Flags इस्तेमाल करने हो तो बीच में OR(|) Operator इस्तेमाल किया जाता है |
Expressions | Description |
---|---|
re.S or re.DOTALL | '.' character हर character को newline के साथ match किया जाता है | |
re.I or re.IGNORECASE | Regular Expression को case-insensitive किया जाता है | |
re.L or re.LOCALE | \w, \W, \b, \B ये current locale का पालन करता है | |
re.M or re.MULTILINE | String की हर line की beginning(^) और end($) match किया जाता है | |
re.U or re.UNICODE | \w,\W,\b,\B,\d,\D ये unicode character rules का पालन करता है | |
re.X or re.VERBOSE | pattern पर # के साथ comment को add करने की अनुमति देता है | |
5. Regular Expression Functions
Functions | Description |
---|---|
compile() | regular expression object पर regular expression pattern को compile किया जाता है | |
findall() | सभी non-overlapping matches list में return करता है | |
finditer() | सभी non-overlapping matches; callable object में return करता है | |
match() | pattern के हिसाब से string के beginning पर ही match करके match object return करता है | |
search() | pattern के हिसाब से string के पहले occurrence को match करके match object return करता है | |
split() | pattern द्वारा string को split करके अलग-अलग substrings को list में return किया जाता है | |
sub() | pattern के हिसाब से string के substring(s) को replace करके नया string return करता है | |
subn() | pattern के हिसाब से string के substring(s) को replace करके replaced string और replaced हुए pattern occurrence(s) को tuple में return करता है | |
6. Regular Expression Object Methods
Object Method के लिए compiled pattern का इस्तेमाल किया जाता है इसके लिए pattern को compile() function में लिखा जाता है |
Object Methods | Description |
---|---|
findall() | दिए हुए pattern के हिसाब से और starting से ending position तक match करके सभी non-overlapping matches को list में return करता है | |
finditer() | दिए हुए pattern के हिसाब से और starting से ending position तक match करके सभी non-overlapping matches को callable object में return करता है | |
match() | दिए हुए pattern के हिसाब से और starting से ending position तक के beginning के match को match object में return करता है | |
search() | दिए हुए pattern के हिसाब से और starting से ending position तक के पहले occurrence को match करके match object में return करता है | |
split() | दिए हुए pattern के हिसाब से string को split करके list में return करता है | |
sub() | pattern के हिसाब से string के substring(s) को replace करके नया string return करता है | |
subn() | pattern के हिसाब से string के substring(s) को replace करके replaced string और replaced हुए pattern occurrence(s) को tuple में return करता है | |
7. Regular Expression Match Object Methods
group() | match के एक या एक से ज्यादा subgroups को return करता है | |
groups() | match के एक या एक से ज्यादा subgroups को tuple में return करता है | |
groupdict() | key और उसके matches के साथ dictionary को return करता है | |
start() | दिए हुए group String पर जहा से match होता है वहा से उसकी index return करता है | |
end() | दिए हुए group String पर जहा तक match होता है वहा से उसकी index return करता है | |