python - 抓取括号内的元素

标签 python regex

如何获取括号内的元素并将它们放入文件中?

我(我) 你(你) 他(他) 她(她)

提前致谢, 阿迪亚

最佳答案

import re

txt = 'me (I) you (You) him (He) her (She)'
words = re.findall('\((.+?)\)', txt)

# words returns: ['I', 'You', 'He', 'She']
with open('filename.txt', 'w') as out:
    out.write('\n'.join(words))

# file 'filename.txt' contains now:

I
You
He
She

关于python - 抓取括号内的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3882407/

相关文章:

javascript - 两位数的正则表达式

python - 使用正则表达式进行标点符号校正

Python:动态定义函数

C++ 嵌入式 python PyArg_ParseTuple 因字符串参数而失败

python - 在迁移中访问 django-custom-user manager 方法

python - 从 GitHub 克隆时 Python 缩进的问题

javascript - 正则表达式验证不允许 home、end、箭头键工作

regex - Perl 文件正则表达式不替换文本

javascript - 正则表达式字符串拆分器

python - 使用Python Imageio在gif中透明背景