提取单引号之间的子串(Extracting substrings between single quotes)
我是python的新手,试图在单引号之间提取子串。 你知道如何用正则表达式做到这一点吗?
EG输入
text = "[(u'apple',), (u'banana',)]"我想提取苹果和香蕉作为['apple', 'banana']等列表项
I am new in python and trying to extract substrings between single quotes. Do you know how to do this with regex?
E.G input
text = "[(u'apple',), (u'banana',)]"I want to extract apple and banana as list items like ['apple', 'banana']
最满意答案
text = "[(u'apple',), (u'banana',)]" print(re.findall(r"\(u'(.*?)',\)", text) ['apple', 'banana'] text = "[(u'this string contains\' an escaped quote mark and\\ an escaped slash',)]" print(re.findall(r"\(u'(.*?)',\)", text)[0]) this string contains' an escaped quote mark and \ an escaped slash text = "[(u'apple',), (u'banana',)]" print(re.findall(r"\(u'(.*?)',\)", text) ['apple', 'banana'] text = "[(u'this string contains\' an escaped quote mark and\\ an escaped slash',)]" print(re.findall(r"\(u'(.*?)',\)", text)[0]) this string contains' an escaped quote mark and \ an escaped slash更多推荐
发布评论