Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python
Python
  1. ความแตกต่างระหว่าง . ? และ * ใน Python Regular Expression

    Special character dot . (Dot) โหมดปกติ จับคู่ตัวอักษรใดก็ตามยกเว้น newline หากระบุ flag DOTALL จะจับคู่รวม newline ด้วย Special character ? ทำให้ RE จับคู่ 0 หรือ 1 ครั้งของ RE ก่อนหน้า ab? จับคู่ a หรือ ab Special character asterisk * ทำให้ RE จับคู่ 0 หรือมากกว่า ของ RE ก่อนหน้า มากที่สุดเท่าที่จะ

  2. เปรียบเทียบ 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 ด้วย (?#

  3. วิธีจำลอง scanf() ใน Python

    ตามเอกสาร Python Python ไม่มี equivalent ของ scanf() Regex ทรงพลังกว่า แต่ verbose กว่า scanf() format strings ตารางด้านล่างแสดง mapping ระหว่าง scanf() tokens และ regex: scanf() TokenRegular Expression %c. %5c.{5} %d[-+]?\d+ %e, %E, %f, %g[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)? %i[-+]?(0[xX][\d

  4. อธิบาย Python Regular Expression Syntax อย่างง่าย

    Regular expression คือลำดับตัวอักษร ใช้หลักๆ สำหรับหาและแทนที่ pattern ในสตริงหรือไฟล์ Python Regex Syntax ใช้ 2 ประเภทตัวอักษร: Meta characters: มีความหมายพิเศษ คล้าย * ใน wildcard Literals: ตัวอักษรธรรมดา เช่น a,b,1,2... Python regular expressions ใช้ raw strings, quantifiers, character classes,

  5. ความแตกต่างระหว่าง re.match() re.search() และ re.findall() ใน Python

    re.match(), re.search(), re.findall() เป็นเมธอดของโมดูล re re.match() หาจับคู่ได้ก็ต่อเมื่อเกิดที่ ต้นสตริง เรียก match() บน TP Tutorials Point TP หา TP จะจับคู่ ตัวอย่าง import re result = re.match(rTP, TP Tutorials Point TP) print(result.group(0)) ผลลัพธ์ TP re.search() คล้าย match() แต่ ไม่จำกั

  6. วิธี Clear Regular Expression Cache ใน Python

    ปัจจุบัน เมื่อ Regular Expressions ถูก Compile ผลลัพธ์จะถูก Cache ไว้ เพื่อให้หาก Regex เดียวกันถูก Compile อีกครั้ง จะถูกดึงจาก Cache โดยไม่ต้องทำงานเพิ่มเติม Cache นี้รองรับได้สูงสุด 100 Entries เมื่อถึง Entry ที่ 100 Cache จะถูก Clear และต้อง Compile ใหม่ วัตถุประสงค์ของ Caching คือเพื่อลด Avera

  7. วิธีจับ exception ที่เกิดจาก Python Regular Expression

    เมื่อใช้ match method หากไม่มี match จะคืน None ไม่มีฟังก์ชันในโมดูล re ที่โยน exception เมื่อ list หรือ matches ว่าง exception re.error โยนเมื่อสตริงที่ส่งให้ฟังก์ชันไม่ใช่ valid regular expression (เช่น parentheses ไม่คู่) หรือเกิด error อื่นระหว่าง compilation หรือ matching ไม่เคยเป็น error หากสต

  8. วิธีหา adverbs ทั้งหมดและตำแหน่งในข้อความด้วย Python Regex

    ตามเอกสาร Python หากต้องการข้อมูลมากกว่าแค่ข้อความที่จับคู่ finditer() มีประโยชน์เพราะคืน match objects แทน strings หากเป็นนักเขียนต้องการหา adverbs ทั้งหมดและตำแหน่งในข้อความ ใช้ finditer() ดังนี้: for m in re.finditer(r\w+ly, text): ... print(%02d-%02d: %s % (m.start(), m.end(), m.group(0)))

  9. วิธีใช้ Special Characters ใน Python Regular Expression

    จากเอกสาร Python: Non-special characters จับคู่ตัวเอง Special characters ไม่จับคู่ตัวเอง: Charความหมาย \Escape special char หรือเริ่ม sequence .จับคู่ตัวอักษรใดก็ตามยกเว้น newline (ดู re.DOTALL) ^จับคู่ต้นสตริง (ดู re.MULTILINE) $จับคู่ท้ายสตริง (ดู re.MULTILINE) [ ]ประกอบชุดตัวอักษรที่จับคู่ได้

  10. วิธีใช้ Wildcard ใน Python Regular Expression

    ใช้ regex . (dot) เป็น wildcard จับคู่ตัวอักษรใดก็ตามยกเว้น newline ตัวอย่าง import re rex = re.compile(th.s) l = this, thus, just, then print(rex.findall(l)) ผลลัพธ์ [this, thus]

  11. วิธีใช้ Range ใน Python Regular Expression

    Range ใน Regular Expressions ช่วงตัวอักษรระบุได้โดยให้ 2 ตัวอักษร คั่นด้วย - เช่น [a-z] จับคู่ตัวพิมพ์เล็ก ASCII ทั้งหมด [0-5][0-9] จับคู่เลข 2 หลัก 00 ถึง 59 ถ้า - ถูก escape (เช่น [a\-z]) หรืออยู่ตัวแรก/สุดท้าย (เช่น [a-]) จะจับคู่ - ตรงตัว [A-Z] จับคู่ตัวพิมพ์ใหญ่ A-Z, [a-c] จับคู่ a-c, [0-9] จับ

  12. ความแตกต่างระหว่าง re.findall() และ re.finditer() ใน Python

    re.findall() ช่วยได้ list ของ pattern ที่จับคู่ทั้งหมด ค้นหาจากต้นหรือท้ายสตริง คืน occurrences ทั้งหมด แนะนำใช้เสมอ ตัวอย่าง import re result = re.search(rTP, TP Tutorials Point TP) print(result.group()) ผลลัพธ์ TP re.finditer() re.finditer(pattern, string, flags=0) คืน iterator ให้ MatchObject i

  13. Backslashes ทำงานอย่างไรใน Python Regular Expressions

    ตามเอกสาร Python metacharacter สำคัญที่สุดใน regex คือ backslash \ เหมือนใน string literals backslash ตามด้วยตัวอักษรต่างๆ เพื่อ special sequences ใช้ escape metacharacters ทั้งหมด เพื่อจับคู่ pattern จริง เช่น จับคู่ [ หรือ \ ให้ขึ้น \[ หรือ \\ ตัวอย่าง import re result = re.search(\d, \d) print(re

  14. วิธีเขียน Python Regular Expression ใช้กับ re.findall()

    re.findall() ช่วยได้ list ของ pattern ที่จับคู่ทั้งหมด ค้นหาจากต้นหรือท้ายสตริง คืน occurrences ทั้งหมด แนะนำใช้ re.findall() เสมอ ทำงานคล้าย search() และ match() ตัวอย่าง import re result = re.search(rTP, TP Tutorials Point TP) print(result.group()) ผลลัพธ์ TP

  15. วิธีเขียน Python Regular Expression ตรวจสอบตัวเลข

    ตัวอย่างตรวจสอบตัวเลขที่เท่ากับ 2018 อย่างแน่นอน ตัวอย่าง import re s = 2018 match = re.match(r\b2018\b, s) print(match.group()) ผลลัพธ์ 2018 ตัวอย่างตรวจสอบจำนวนเต็มบวก 5 หลักใดๆ ตัวอย่าง import re s = 2346 match = re.match(r(?

Total 9001 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:38/451  20-คอมพิวเตอร์/Page Goto:1 32 33 34 35 36 37 38 39 40 41 42 43 44