Regex मेटा-कैरेक्टर्स

Python में Regular Expressions

Maria Eugenia Inzaugarat

Data Scientist

पैटर्न ढूँढना

मैच ढूँढने की दो अलग ऑपरेशंस:

re.search(r"\d{4}", "4506 people attend the show")
<re.Match object; span=(0, 4), match='4506'>

 

re.search(r"\d+", "Yesterday, I saw 3 shows")
<re.Match object; span=(17, 18), match='3'>

 

re.match(r"\d{4}", "4506 people attend the show")
<re.Match object; span=(0, 4), match='4506'>

 

re.match(r"\d+","Yesterday, I saw 3 shows")
None
Python में Regular Expressions

स्पेशल कैरेक्टर्स

  • किसी भी कैरेक्टर से मैच (newline छोड़कर): .

 

my_links = "Just check out this link: www.amazingpics.com. It has amazing photos!"

re.findall(r"www com", my_links)
Python में Regular Expressions

स्पेशल कैरेक्टर्स

  • किसी भी कैरेक्टर से मैच (newline छोड़कर): .

 

my_links = "Just check out this link: www.amazingpics.com. It has amazing photos!"

re.findall(r"www.+com", my_links)
['www.amazingpics.com']
Python में Regular Expressions

स्पेशल कैरेक्टर्स

  • स्ट्रिंग की शुरुआत: ^
my_string = "the 80s music was much better that the 90s"
re.findall(r"the\s\d+s", my_string)
['the 80s', 'the 90s']

 

re.findall(r"^the\s\d+s", my_string)
['the 80s']
Python में Regular Expressions

स्पेशल कैरेक्टर्स

  • स्ट्रिंग का अंत: $
my_string = "the 80s music hits were much better that the 90s"
re.findall(r"the\s\d+s$", my_string)
['the 90s']
Python में Regular Expressions

स्पेशल कैरेक्टर्स

  • स्पेशल कैरेक्टर्स को एस्केप करें: \
my_string = "I love the music of Mr.Go. However, the sound was too loud."
print(re.split(r".\s", my_string))
['', 'lov', 'th', 'musi', 'o', 'Mr.Go', 'However', 'th', 'soun', 'wa', 'to', 'loud.']

 

print(re.split(r"\.\s", my_string))
['I love the music of Mr.Go', 'However, the sound was too loud.']
Python में Regular Expressions

OR ऑपरेटर

  • कैरेक्टर: |
my_string = "Elephants are the world's largest land animal! I would love to see an elephant one day"
re.findall(r"Elephant|elephant", my_string)
['Elephant', 'elephant']
Python में Regular Expressions

OR ऑपरेटर

  • कैरेक्टर्स का सेट: [ ]
my_string = "Yesterday I spent my afternoon with my friends: MaryJohn2 Clary3"
re.findall(r"[a-zA-Z]+\d", my_string)
['MaryJohn2', 'Clary3']
Python में Regular Expressions

OR ऑपरेटर

  • कैरेक्टर्स का सेट: [ ]
my_string = "My&name&is#John Smith. I%live$in#London."
re.sub(r"[#$%&]", " ", my_string)
'My name is John Smith. I live in London.'
Python में Regular Expressions

OR ओपरेन्ड

  • कैरेक्टर्स का सेट: [ ]
    • ^ एक्सप्रेशन को नेगेटिव बना देता है

 

my_links = "Bad website: www.99.com. Favorite site: www.hola.com"
re.findall(r"www[^0-9]+com", my_links)
['www.hola.com']
Python में Regular Expressions

अभ्यास करते हैं!

Python में Regular Expressions

Preparing Video For Download...