gpt4 book ai didi

python - 正则表达式在另一个词之前添加字符(来自选择列表)

转载 作者:太空宇宙 更新时间:2023-11-04 10:32:54 25 4
gpt4 key购买 nike

我正在阅读 this article ,这给了我使用组的想法。我想在/O、/ORGANIZATION、/PEOPLE 或/LOCATION 之后的字符前添加一个\t

我有以下内容

'The/O\nSkoll/ORGANIZATION\nFoundation/ORGANIZATION\n,/O\nbased/O\nin/O\nSilicon/LOCATION\nValley/LOCATION\na'

并想要以下内容

The\t/O\nSkoll\t/ORGANIZATION\nFoundation\tORGANIZATION\n

我试过了,但是不行。我如何记忆起正则表达式捕获的是哪个组织?

x = str(t)
x = re.sub('\/(ORGANIZATION|LOCATION|PERSON|O)','\t\1', x)

我的中间解决方案,但最好有一个单行代码。

x = re.sub(r'\/(ORGANIZATION)',r'\t\1', x)
x = re.sub(r'\/(LOCATION)', r'\t\1',x)
x = re.sub(r'\/(PERSON)',r'\t\1', x)
x = re.sub(r'\/(O)',r'\t\1', x)

最佳答案

像这样:

>>> t = 'The/O\nSkoll/ORGANIZATION\nFoundation/ORGANIZATION\n,/O\nbased/O\nin/O\nSilicon/LOCATION\nValley/LOCATION\na'
>>> re.sub(r'(/(?:ORGANIZATION|LOCATION|PERSON|O))',r'\t\1', t)
'The\t/O\nSkoll\t/ORGANIZATION\nFoundation\t/ORGANIZATION\n,\t/O\nbased\t/O\nin\t/O\nSilicon\t/LOCATION\nValley\t/LOCATION\na'

演示: http://regex101.com/r/nB5dN3/1

关于python - 正则表达式在另一个词之前添加字符(来自选择列表),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25245971/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com