Lookaround

Python में Regular Expressions

Maria Eugenia Inzaugarat

Data Scientist

इधर-उधर देखना

  • यह जाँचने देता है कि कोई sub-pattern मुख्य पैटर्न के आगे है या पीछे

   

Python में Regular Expressions

इधर-उधर देखना

  • यह जाँचने देता है कि कोई sub-pattern मुख्य पैटर्न के आगे है या पीछे

 

मैचिंग प्रक्रिया में मेरी मौजूदा स्थिति पर, आगे या पीछे देखकर जाँचें कि कोई पैटर्न मैच करता है या नहीं, फिर आगे बढ़ें.

Python में Regular Expressions

Look-ahead

  • Non-capturing group
  • यह जाँचती है कि expression का पहला भाग lookahead expression के साथ आता है या नहीं
  • केवल expression का पहला भाग लौटाएँ

Python में Regular Expressions

Positive look-ahead

  • Non-capturing group
  • यह जाँचती है कि expression का पहला भाग lookahead expression के साथ आता है
  • केवल expression का पहला भाग लौटाएँ

 

my_text = "tweets.txt transferred, mypass.txt transferred, keywords.txt error"

re.findall(r"\w+\.txt ", my_text)
Python में Regular Expressions

Positive look-ahead

  • Non-capturing group
  • यह जाँचती है कि expression का पहला भाग lookahead expression के साथ आता है
  • केवल expression का पहला भाग लौटाएँ

 

my_text = "tweets.txt transferred, mypass.txt transferred, keywords.txt error"

re.findall(r"\w+\.txt(?=\stransferred)", my_text)
['tweets.txt', 'mypass.txt']
Python में Regular Expressions

Negative look-ahead

  • Non-capturing group
  • यह जाँचती है कि expression का पहला भाग lookahead expression के साथ नहीं आता
  • केवल expression का पहला भाग लौटाएँ

 

my_text = "tweets.txt transferred, mypass.txt transferred, keywords.txt error"

re.findall(r"\w+\.txt ", my_text)
Python में Regular Expressions

Negative look-ahead

  • Non-capturing group
  • सभी matches लें जो किसी विशेष पैटर्न से पहले हों या न हों
  • look-behind expression के बाद वाला पैटर्न लौटाएँ

 

my_text = "tweets.txt transferred, mypass.txt transferred, keywords.txt error"

re.findall(r"\w+\.txt(?!\stransferred)", my_text)
['keywords.txt']
Python में Regular Expressions

Look-behind

  • Non-capturing group
  • वे सारे matches पाएँ जो किसी विशेष पैटर्न से पहले हों या न हों.
  • look-behind expression के बाद वाला पैटर्न लौटाएँ

Python में Regular Expressions

Positive look-behind

  • Non-capturing group
  • वे सारे matches पाएँ जो किसी विशेष पैटर्न से पहले हों.
  • look-behind expression के बाद वाला पैटर्न लौटाएँ

 

my_text = "Member: Angus Young, Member: Chris Slade, Past: Malcolm Young, Past: Cliff Williams."

re.findall(r" \w+\s\w+", my_text)
Python में Regular Expressions

Positive look-behind

  • Non-capturing group
  • वे सारे matches पाएँ जो किसी विशेष पैटर्न से पहले हों.
  • look-behind expression के बाद वाला पैटर्न लौटाएँ

 

my_text = "Member: Angus Young, Member: Chris Slade, Past: Malcolm Young, Past: Cliff Williams."

re.findall(r"(?<=Member:\s)\w+\s\w+", my_text)
['Angus Young', 'Chris Slade']
Python में Regular Expressions

Negative look-behind

  • Non-capturing group
  • वे सारे matches पाएँ जो किसी विशेष पैटर्न से पहले हों.
  • look-behind expression के बाद वाला पैटर्न लौटाएँ

 

my_text = "My white cat sat at the table. However, my brown dog was lying on the couch."

re.findall(r"(?<!brown\s)(cat|dog)", my_text)
['cat']
Python में Regular Expressions

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

Python में Regular Expressions

Preparing Video For Download...