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

วิธีจับคู่ทุกอย่างยกเว้น 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 0x00000000048965E0>
None
None