การทำซ้ำ

Regular Expressions ใน Python

Maria Eugenia Inzaugarat

Data Science

อักขระที่ซ้ำกัน

ตรวจสอบสตริงต่อไปนี้:

Regular Expressions ใน Python

อักขระที่ซ้ำกัน

ตรวจสอบสตริงต่อไปนี้:

Regular Expressions ใน Python

อักขระที่ซ้ำกัน

ตรวจสอบสตริงต่อไปนี้:

Regular Expressions ใน Python

อักขระที่ซ้ำกัน

ตรวจสอบสตริงต่อไปนี้:

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'>
Regular Expressions ใน Python

อักขระที่ซ้ำกัน

ตรวจสอบสตริงต่อไปนี้:

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

Quantifiers:

metacharacter ที่บอก regex engine
ว่าต้องจับคู่อักขระทางซ้ายของมัน
กี่ครั้ง

Regular Expressions ใน Python

Quantifiers

  • หนึ่งครั้งขึ้นไป: +

 

text = "Date of start: 4-3. Date of registration: 10-04."
re.findall(r"       ", text)
Regular Expressions ใน Python

Quantifiers

  • หนึ่งครั้งขึ้นไป: +

 

text = "Date of start: 4-3. Date of registration: 10-04."
re.findall(r"\d+-   ", text)
Regular Expressions ใน Python

Quantifiers

  • หนึ่งครั้งขึ้นไป: +

 

text = "Date of start: 4-3. Date of registration: 10-04."
re.findall(r"\d+-\d+", text)
['4-3', '10-04']
Regular Expressions ใน Python

Quantifiers

  • ศูนย์ครั้งขึ้นไป: *

 

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

re.findall(r"@\w+\W*\w+", my_string)
['@ameli!a', '@joh&&n', '@mary90']
Regular Expressions ใน Python

Quantifiers

  • ศูนย์ครั้งหรือหนึ่งครั้ง: ?

 

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

re.findall(r"colou?r", text)
['color', 'colour']
Regular Expressions ใน Python

Quantifiers

  • อย่างน้อย n ครั้ง สูงสุด m ครั้ง: {n, m}

 

phone_number = "John: 1-966-847-3131 Michelle: 54-908-42-42424"
re.findall(r"                          ", phone_number)
Regular Expressions ใน Python

Quantifiers

  • อย่างน้อย n ครั้ง สูงสุด m ครั้ง: {n, m}

 

phone_number = "John: 1-966-847-3131 Michelle: 54-908-42-42424"
re.findall(r"\d{1,2}-                 ", phone_number)
Regular Expressions ใน Python

Quantifiers

  • อย่างน้อย n ครั้ง สูงสุด m ครั้ง: {n, m}

 

phone_number = "John: 1-966-847-3131 Michelle: 54-908-42-42424"
re.findall(r"\d{1,2}-\d{3}-           ", phone_number)
Regular Expressions ใน Python

Quantifiers

  • อย่างน้อย n ครั้ง สูงสุด m ครั้ง: {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']
Regular Expressions ใน Python

Quantifiers

  • ทางซ้ายทันที

    • r"apple+": + ใช้กับ e ไม่ใช่ apple
Regular Expressions ใน Python

มาฝึกกันเถอะ!

Regular Expressions ใน Python

Preparing Video For Download...