Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.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 3.x 如何使用python打印行中的特定字符?_Python 3.x_Regex - Fatal编程技术网

Python 3.x 如何使用python打印行中的特定字符?

Python 3.x 如何使用python打印行中的特定字符?,python-3.x,regex,Python 3.x,Regex,我正在尝试编写一个脚本,以打印特定字符串后的特定单词 这是输入文件 Theyare "playing in the ground", with friends Theyare "going to Paris", with family Theyare "motivating to learn new things", by themselves 在输出中,我试图选择“are”作为关键字,并在“are”之后选择“我想要”中的文本,并且我

我正在尝试编写一个脚本,以打印特定字符串后的特定单词

这是输入文件

Theyare "playing in the ground", with friends
Theyare "going to Paris", with family
Theyare "motivating to learn new things", by themselves
在输出中,我试图选择“are”作为关键字,并在“are”之后选择“我想要”中的文本,并且我想要将空格之前的文本添加到“”中

输出应该是

They playing in the ground
They going to Paris
They motivating to learn new things
我可以用下面的代码打印行的其余部分,但不能打印某些单词。到目前为止我有

with open ('input.txt', 'r') as f:
    for lines in f:
        a = re.search(r'\bare', f):
            if a:
                print (lines)
            

任何帮助都将不胜感激

使用正则表达式提取所需行的部分

with open ('input.txt', 'r') as f:
    for lines in f:
        m = re.match(r'(.*?) are "(.*?)"')
        if m:
            print m.group(1) + " " + m.group(2)

m
中的组返回与
()

之间的模式相匹配的行部分。如果您的行始终与您提供的示例类似,则可以使用字符串操作:

s='他们在“地上玩”,和朋友们一起玩'
are_split=s.split('are')
#是分裂=['他们','在地上玩',和朋友']
quote_split=are_split[1]。拆分(“”)
#quote_split=[''‘在地上玩’,'和朋友一起玩']
打印(正在拆分[0]+引用拆分[1])
#“他们在地上玩”