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

标签 python regex string

我正在阅读 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/

相关文章:

c - 打印 "p+p[3]-p[1]"的值

python - 为什么会超出范围?

python - 在python中遍历列表的最有效方法是什么?

ios - 正则表达式中应该转义什么字符

regex - elasticsearch span_near查询错误命中

python - 在 Python 中使用正则表达式将文本拆分为句子

c++ - 如何将字符串数组的值分配给 "Name"+ #,例如。名称 1、名称 2、名称 3 等。C++

python - astropy.io.fits - HIERARCH 关键字不适用于 CONTINUE 卡 : Bug or "Feature" of the FITS standard?

python - BCELoss 用于二进制像素级分割 pytorch

Python - 解析字符串并将其转换为列表