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

วิธี Split String ด้วย Multiple Delimiters ใน Python

ใช้ re.split(delimiter_regex, string)

ตัวอย่าง

a = 'Beautiful, is; better*than\nugly'
import re
print(re.split('; |, |\*|\n', a))

ผลลัพธ์

['Beautiful', 'is', 'better', 'than', 'ugly']

Regex pattern ใช้ | คั่น delimiters ต่างๆ