Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

เปรียบเทียบ Regular Expressions ใน Perl และ Python

คุณสมบัติ regex พื้นฐานเกือบเหมือนกันในทุก implementation: wildcard ., quantifiers * + ?, anchors ^ $, character classes [], back references \1 \2 \3

  • Alternation ใช้ | ในทั้ง Perl และ Python
  • ทั้งคู่แก้ไข regex ด้วย (?aimsx) เช่น (?i) case-insensitive ความหมายเหมือนกัน ทั้งคู่รองรับ comment ด้วย (?# ... )
  • รองรับ positive/negative look-around syntax เหมือนกัน: (?=) (?!) (?<=) (?
  • รองรับ anchors \A \Z และ character classes \d \D \s \S
  • ทั้งคู่ตั้งชื่อ capture ด้วย (?P<name>) อ้างอิงด้วย (?P=name) Perl มี syntax ของตัวเองเพิ่ม นอกจากรองรับ syntax ของ Python