【发布时间】:2018-03-25 03:27:49
【问题描述】:
我有一个字符串,例如this is title [[this is translated title]],我需要提取这两个子字段。 this is title, this is translated title
我尝试使用正则表达式,但无法完成。
def translate(value):
# Values are paseed in the form of
# "This is text [[This is translated text]]"
import re
regex = r"(.+)(\[\[.*\]\])"
match = re.match(regex, value)
# Return text
first = match.group(1)
# Return translated text
second = match.group(2).lstrip("[[").rstrip("]]")
return first, second
但这会失败。当字符串为“简单纯文本”时
【问题讨论】:
-
你所拥有的似乎有效。有什么问题?
标签: python regex python-3.x python-3.5