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

วิธีจับคู่ whitespace แต่ไม่จับ newlines ด้วย Python Regex

ใช้ regex จับคู่ whitespace เฉพาะ space/tab แต่ไม่รวม newlines ในสตริงที่กำหนด

ตัวอย่าง

import re
print(re.findall(r"(\s){1,}", """I find     Tutorialspoint useful"""))

ผลลัพธ์

[' ', ' ', ' ']