Alternation और non-capturing groups

Python में Regular Expressions

Maria Eugenia Inzaugarat

Data Scientist

Pipe

  • Vertical bar या pipe: |

 

my_string = "I want to have a pet. But I don't know if I want a cat, a dog or a bird."

re.findall(r"cat|dog|bird", my_string)
['cat', 'dog', 'bird']
Python में Regular Expressions

Pipe

  • Vertical bar या pipe: |

   

my_string = "I want to have a pet. But I don't know if I want 2 cats, 1 dog or a bird."

re.findall(r"\d+\scat|dog|bird", my_string)
['2 cat', 'dog', 'bird']

Python में Regular Expressions

Alternation

  • वैकल्पिक patterns में से चुनने के लिए groups का उपयोग करें

 

my_string = "I want to have a pet. But I don't know if I want 2 cats, 1 dog or a bird."
re.findall(r"\d+\s(cat|dog|bird)", my_string)
['cat', 'dog']
Python में Regular Expressions

Alternation

  • वैकल्पिक patterns में से चुनने के लिए groups का उपयोग करें

 

my_string = "I want to have a pet. But I don't know if I want 2 cats, 1 dog or a bird."

re.findall(r"(\d)+\s(cat|dog|bird)", my_string)
[('2', 'cat'), ('1', 'dog')]
Python में Regular Expressions

Non-capturing groups

  • किसी group को match करें पर capture न करें

    • जब group को backreference नहीं किया जाता

    • ?: जोड़ें: (?:regex)

Python में Regular Expressions

Non-capturing groups

  • किसी group को match करें पर capture न करें

 

my_string = "John Smith: 34-34-34-042-980, Rebeca Smith: 10-10-10-434-425"

re.findall(r"(?:\d{2}-){3}(\d{3}-\d{3})", my_string)
['042-980', '434-425']
Python में Regular Expressions

Alternation

  • Alternation के लिए non-capturing groups का उपयोग करें

 

my_date = "Today is 23rd May 2019. Tomorrow is 24th May 19."

re.findall(r"(\d+)(?:th|rd)", my_date)
['23', '24']
Python में Regular Expressions

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

Python में Regular Expressions

Preparing Video For Download...