Lookaround

Regular Expressions ใน Python

Maria Eugenia Inzaugarat

Data Scientist

การมองรอบข้าง

  • ช่วยตรวจสอบว่า sub-pattern อยู่ข้างหน้าหรือข้างหลัง pattern หลัก

   

Regular Expressions ใน Python

การมองรอบข้าง

  • ช่วยตรวจสอบว่า sub-pattern อยู่ข้างหน้าหรือข้างหลัง pattern หลัก

 

ณ ตำแหน่งปัจจุบันในกระบวนการจับคู่ ให้มองไปข้างหน้าหรือข้างหลังเพื่อตรวจสอบว่า pattern ใด pattern หนึ่งตรงหรือไม่ตรงก่อนดำเนินการต่อ

Regular Expressions ใน Python

Look-ahead

  • กลุ่มที่ไม่จับค่า (Non-capturing group)
  • ตรวจสอบว่าส่วนแรกของนิพจน์ตามด้วยหรือไม่ตามด้วย lookahead expression
  • คืนค่าเฉพาะส่วนแรกของนิพจน์

Regular Expressions ใน Python

Positive look-ahead

  • กลุ่มที่ไม่จับค่า (Non-capturing group)
  • ตรวจสอบว่าส่วนแรกของนิพจน์ตามด้วย lookahead expression
  • คืนค่าเฉพาะส่วนแรกของนิพจน์

 

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

re.findall(r"\w+\.txt ", my_text)
Regular Expressions ใน Python

Positive look-ahead

  • กลุ่มที่ไม่จับค่า (Non-capturing group)
  • ตรวจสอบว่าส่วนแรกของนิพจน์ตามด้วย lookahead expression
  • คืนค่าเฉพาะส่วนแรกของนิพจน์

 

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

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

Negative look-ahead

  • กลุ่มที่ไม่จับค่า (Non-capturing group)
  • ตรวจสอบว่าส่วนแรกของนิพจน์ไม่ตามด้วย lookahead expression
  • คืนค่าเฉพาะส่วนแรกของนิพจน์

 

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

re.findall(r"\w+\.txt ", my_text)
Regular Expressions ใน Python

Negative look-ahead

  • กลุ่มที่ไม่จับค่า (Non-capturing group)
  • ตรวจสอบว่าส่วนแรกของนิพจน์ไม่ตามด้วย lookahead expression
  • คืนค่าเฉพาะส่วนแรกของนิพจน์

 

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

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

Look-behind

  • กลุ่มที่ไม่จับค่า (Non-capturing group)
  • ดึงผลลัพธ์ที่มีหรือไม่มี pattern นำหน้า
  • คืนค่า pattern ที่อยู่หลัง look-behind expression

Regular Expressions ใน Python

Positive look-behind

  • กลุ่มที่ไม่จับค่า (Non-capturing group)
  • ดึงผลลัพธ์ที่มี pattern นำหน้าเฉพาะที่กำหนด
  • คืนค่า pattern ที่อยู่หลัง 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)
Regular Expressions ใน Python

Positive look-behind

  • กลุ่มที่ไม่จับค่า (Non-capturing group)
  • ดึงผลลัพธ์ที่มี pattern นำหน้าเฉพาะที่กำหนด
  • คืนค่า pattern ที่อยู่หลัง 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']
Regular Expressions ใน Python

Negative look-behind

  • กลุ่มที่ไม่จับค่า (Non-capturing group)
  • ดึงผลลัพธ์ที่ไม่มี pattern นำหน้าเฉพาะที่กำหนด
  • คืนค่า pattern ที่อยู่หลัง 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']
Regular Expressions ใน Python

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

Regular Expressions ใน Python

Preparing Video For Download...