Repetiții

Expresii regulate în Python

Maria Eugenia Inzaugarat

Data Science

Caractere repetate

Validați următorul șir:

Expresii regulate în Python

Caractere repetate

Validați următorul șir:

Expresii regulate în Python

Caractere repetate

Validați următorul șir:

Expresii regulate în Python

Caractere repetate

Validați următorul șir:

import re
password = "password1234"
re.search(r"\w\w\w\w\w\w\w\w\d\d\d\d", password)
<re.Match object; span=(0, 12), match='password1234'>
Expresii regulate în Python

Caractere repetate

Validați următorul șir:

import re
password = "password1234"
re.search(r"\w{8}\d{4}", password)
<re.Match object; span=(0, 12), match='password1234'>

Cuantificatori:

Un metacaracter care indică motorului regex
de câte ori să potrivească
caracterul imediat din stânga sa.

Expresii regulate în Python

Cuantificatori

  • O dată sau mai mult: +

 

text = "Date of start: 4-3. Date of registration: 10-04."
re.findall(r"       ", text)
Expresii regulate în Python

Cuantificatori

  • O dată sau mai mult: +

 

text = "Date of start: 4-3. Date of registration: 10-04."
re.findall(r"\d+-   ", text)
Expresii regulate în Python

Cuantificatori

  • O dată sau mai mult: +

 

text = "Date of start: 4-3. Date of registration: 10-04."
re.findall(r"\d+-\d+", text)
['4-3', '10-04']
Expresii regulate în Python

Cuantificatori

  • De zero sau mai multe ori: *

 

my_string = "The concert was amazing! @ameli!a @joh&&n @mary90"

re.findall(r"@\w+\W*\w+", my_string)
['@ameli!a', '@joh&&n', '@mary90']
Expresii regulate în Python

Cuantificatori

  • De zero ori sau o dată: ?

 

text = "The color of this image is amazing. However, the colour blue could be brighter."

re.findall(r"colou?r", text)
['color', 'colour']
Expresii regulate în Python

Cuantificatori

  • Cel puțin n ori, cel mult m ori: {n, m}

 

phone_number = "John: 1-966-847-3131 Michelle: 54-908-42-42424"
re.findall(r"                          ", phone_number)
Expresii regulate în Python

Cuantificatori

  • Cel puțin n ori, cel mult m ori: {n, m}

 

phone_number = "John: 1-966-847-3131 Michelle: 54-908-42-42424"
re.findall(r"\d{1,2}-                 ", phone_number)
Expresii regulate în Python

Cuantificatori

  • Cel puțin n ori, cel mult m ori: {n, m}

 

phone_number = "John: 1-966-847-3131 Michelle: 54-908-42-42424"
re.findall(r"\d{1,2}-\d{3}-           ", phone_number)
Expresii regulate în Python

Cuantificatori

  • Cel puțin n ori, cel mult m ori: {n, m}

 

phone_number = "John: 1-966-847-3131 Michelle: 54-908-42-42424"
re.findall(r"\d{1,2}-\d{3}-\d{2,3}-\d{4,}", phone_number)
['1-966-847-3131', '54-908-42-42424']
Expresii regulate în Python

Cuantificatori

  • Imediat la stânga

    • r"apple+": + se aplică lui e, nu lui apple
Expresii regulate în Python

Să exersăm!

Expresii regulate în Python

Preparing Video For Download...