Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python
Python
  1. ความแตกต่างระหว่าง re.search() และ re.findall() ใน Python Regex

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

  2. วิธีใช้ตัวแปรใน Python Regular Expression

    ตัวอย่างการใช้ตัวแปรใน Python regex ตัวแปรต้องไม่มี metacharacters หรือ special characters ใช้ string concatenation สร้าง pattern ตัวอย่าง import re s = I love books var_name = love result = re.search((.+) + var_name + (.+), s) print(result) var_name = hate s2 = I hate books result = re.search((.+)

  3. ความหมายของ ?: ใน Python Regular Expression

    Non-capturing Groups (กลุ่มที่ไม่จับคู่) หากไม่ต้องการให้กลุ่มจับคู่ผลลัพธ์ ให้เขียน regex เป็น Set(?:Value) เครื่องหมาย ? และ : หลังวงเล็บเปิด คือ syntax สร้าง non-capturing group Regex Set(Value)? จับคู่ Set หรือ SetValue กรณีแรก capturing group แรกว่าง กรณีที่สองจับคู่ Value ? ต่อท้ายคือ quantifi

  4. วิธีใช้ Named Groups ใน Python Regular Expression

    Named Groups (กลุ่มที่มีชื่อ) Regex engine รุ่นใหม่รองรับ numbered capturing groups และ backreferences แต่ regex ยาวที่มีหลายกลุ่มและ backreferences อ่านและเข้าใจยาก การเพิ่ม/ลบกลุ่มกลาง regex จะรบกวนหมายเลขกลุ่มที่ตามมา โมดูล re ของ Python เป็นตัวแรกที่เสนอ named capturing groups และ named backrefe

  5. วิธีจับคู่ parentheses ใน Python Regular Expression

    ใช้ regex จับคู่ parentheses ในสตริง s แล้วลบ parentheses ออกจากสตริง s1 ตัวอย่าง import re s = I love book() result = re.search(r\(\), s) print(result.group()) s1 = I love book(s) result2 = re.sub(r[\(\)], , s1) print(result2) ผลลัพธ์ () I love books

  6. อธิบาย Python Regular Expression: search vs match

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

  7. Recursion และ Backtracking ใน Python คืออะไร

    Recursion (การเรียกซ้ำ) Recursion ช่วยแบ่งและแก้ปัญหา แต่ละ recursive call สปินออฟ recursive calls อื่นๆ จุดศูนย์กลางของ recursive function มี 2 กรณี: base cases (บอกให้ recursion หยุด) และ recursive cases0 Backtracking (การย้อนกลับ) Backtracking เป็นอัลกอริทึมทั่วไปสำหรับหาคำตอบปัญหาคำนวณ บางอย่าง

  8. วิธี strip spaces/tabs/newlines ด้วย Python Regular Expression

    ใช้ regex strip spaces/tabs/newlines จากสตริง ใช้ \S แทน non-whitespace characters ทั้งหมด ตัวอย่าง import re print(re.findall(r[\S], I find Tutorialspoint useful)) ผลลัพธ์ [I, f, i, n, d, T, u, t, o, r, i, a, l, s, p, o, i, n, t, u, s, e, f, u, l]

  9. วิธีจับคู่ทุกอย่างยกเว้น space และ newline ด้วย Python Regex

    ใช้ regex จับคู่ทุกอย่างยกเว้น space และ newline ในสตริงที่กำหนด ตัวอย่าง import re print(re.match(r^[^ \n]*$, IfindTutorialspointuseful)) print(re.match(r^[^ \n]*$, I find Tutorialspointuseful)) print(re.match(r^[^ \n]*$, Ifind Tutorialspointuseful)) ผลลัพธ์ <_sre.SRE_Match object at 0x00000000

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