Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中的子字符串解析_Python_Python 2.7 - Fatal编程技术网

python中的子字符串解析

python中的子字符串解析,python,python-2.7,Python,Python 2.7,我想用Python解析具有特定模式的子字符串 例如: 我想从以[]开头的行中提取引号中的字符串。我不需要引号中的所有字符串。在Python中如何执行此操作?不确定行的来源,但是否每行都不同 import re lines=['[ AA BB ] (CC) DD "String to extract"',"Another string in Next line",'XX "Another string"'] for line in lines: if line.startswith("["

我想用Python解析具有特定模式的子字符串

例如:


我想从以[]开头的行中提取引号中的字符串。我不需要引号中的所有字符串。在Python中如何执行此操作?

不确定行的来源,但是否每行都不同

import re
lines=['[ AA BB ] (CC) DD "String to extract"',"Another string in Next line",'XX "Another string"']
for line in lines:
    if line.startswith("["):
        print re.findall(r'\"(.+?)\"',line)
['String to extract']

不知道你的消息来源

myStrings = []
for line in some_iterable:
    if line.startswith('['):
        myStrings.append(line.split('"')[1]


使用正则表达式RegExwhat strings您想要什么字符串?我只想提取要从第1行提取的字符串,因为该行以[
myStrings = []
for line in some_iterable:
    if line.startswith('['):
        myStrings.append(line.split('"')[1]
myStrings = [l.split('"')[1] for l in some_iterable if l.startswith('[')]