Python: Split Line by Regex

By Xah Lee. Date: . Last updated: .

this page shows you how to split a line by regex.

let's say you have a file like this:

你是我最苦澀的等待   |   you are my hardest wait
讓我歡喜又害怕未來   |   giving me joy and also fear the future

and you want to get just the Chinese lines.

# split lines example
# get just chinese part in each line

import re

myText = r"""你是我最苦澀的等待   |   you are my hardest wait
讓我歡喜又害怕未來   |   giving me joy and also fear the future"""

# split into lines
myLines = re.split(r'\n', myText)

for aLine in myLines:
    lineParts = re.split(r'\s*\|\s*', aLine, re.U)
    print(lineParts[0])

# prints:
# 你是我最苦澀的等待
# 讓我歡喜又害怕未來