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

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

ใช้ regex จับคู่และพิมพ์ space และ newline เท่านั้นในสตริงที่กำหนด

ตัวอย่าง

import re
foo = ' I find     Tutorialspoint     useful'
result = re.findall(r'\s+', foo)
print(result)

ผลลัพธ์

[' ', ' ', '\n', '\n']