Lab 1: การค้นหาและสกัด Email จากไฟล์ข้อความด้วย Regular Expression

คำสั่ง

จงเขียนโปรแกรมภาษา Python เพื่อค้นหาและสกัดข้อมูล email address จากไฟล์ข้อความที่มีหลายบรรทัด โดยใช้ Regular Expression

คำแนะนำ:


วัตถุประสงค์

  1. เพื่อเรียนรู้การใช้งาน Regular Expression (Regex) ในภาษา Python
  2. เพื่อฝึกทักษะการอ่านและประมวลผลไฟล์ข้อความ
  3. เพื่อสามารถค้นหาและสกัดข้อมูล email จากข้อความได้อย่างมีประสิทธิภาพ

ตัวอย่างข้อมูล

ไฟล์ sample_text.txt:

สวัสดีครับ ผมชื่อสมชาย
Email: somchai.test@example.com
โทร: 081-234-5678

ติดต่อทีมงานได้ที่:
- ฝ่ายขาย: sales@company.co.th
- ฝ่ายสนับสนุน: support@company.co.th
- CEO: ceo.admin@company.com

สำหรับข้อมูลเพิ่มเติมติดต่อ:
john.doe123@gmail.com หรือ jane_smith@university.ac.th

Email ที่ไม่ถูกต้อง: invalid@, @invalid.com, test@.com

ตัวอย่างผลลัพธ์

กำลังค้นหา email จากไฟล์: sample_text.txt
============================================================

พบ email ทั้งหมด 6 รายการ:

✓ บรรทัดที่   2: somchai.test@example.com
✓ บรรทัดที่   6: sales@company.co.th
✓ บรรทัดที่   7: support@company.co.th
✓ บรรทัดที่   8: ceo.admin@company.com
✓ บรรทัดที่  11: john.doe123@gmail.com
✓ บรรทัดที่  11: jane_smith@university.ac.th

============================================================
Email ที่ไม่ซ้ำกัน (6 รายการ):
  - ceo.admin@company.com
  - jane_smith@university.ac.th
  - john.doe123@gmail.com
  - sales@company.co.th
  - somchai.test@example.com
  - support@company.co.th

============================================================
สถิติ Domain:
  - company.co.th: 2 email(s)
  - example.com: 1 email(s)
  - company.com: 1 email(s)
  - gmail.com: 1 email(s)
  - university.ac.th: 1 email(s)