简体   繁体   English

提取特定字符之间的子字符串

[英]Extract substring between specific characters

I have some strings like: 我有一些像这样的字符串:

\i{}Agrostis\i0{} <L.>

I would like to get rid of the '\\i{}', '\\io{}' characters, so that I could get just: 我想摆脱'\\ i {}','\\ io {}'字符,这样我就可以得到:

Agrostis <L.>

I've tried the following code (adapted from here ): 我尝试了以下代码(从此处改编):

m = re.search('\i{}(.+?)\i0', item_name)
if m:
   name = m.group(1).strip('\\')
else:
   name = item_name

It works in part, because when I run it I get just: 它部分起作用,因为当我运行它时,我得到的只是:

Agrostis

without the 没有

<L.>

part (which I want to keep). 部分(我想保留)。

Any hints? 有什么提示吗?

Thanks in advance for any assistance you can provide! 预先感谢您可以提供的任何帮助!

使用s.replace('\\i{}', '')s.replace('\\io{}', '')

You ca do this in different ways. 您可以通过不同的方式执行此操作。

The simplest one is to use str.replace 最简单的一种是使用str.replace

s = '''\i{}Agrostis\i0{} <L.>'''
s2 = s.replace('''\i{}''', '').replace('''\i0{}''', '')

Another way is to use re.sub() 另一种方法是使用re.sub()

You need to use the re.sub function. 您需要使用re.sub函数。

In [34]: import re

In [35]: s = "\i{}Agrostis\i0{} <L.>"

In [36]: re.sub(r'\\i\d*{}', '', s)
Out[36]: 'Agrostis <L.>'

You could use a character class along with re.sub() 您可以将字符类re.sub()一起使用

import re
regex = r'\\i[\d{}]+'
string = "\i{}Agrostis\i0{} <L.>"

string = re.sub(regex, '', string)
print string

See a demo on ideone.com . 在ideone.com上查看演示

You can either use s.replace('\\i{}', '') and s.replace('\\io{}', '') , as Julien said, or, continuing with the regex approach, change your pattern to: 您可以使用s.replace('\\i{}', '')s.replace('\\io{}', '') ,或者继续使用正则表达式方法,将模式更改为:

re.search('\\i{}(.+?)\\i0(.++)', item_name)

And use m.group(1).strip('\\\\') + m.group(2).strip('\\\\') as the result. 并使用m.group(1).strip('\\\\') + m.group(2).strip('\\\\')作为结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 提取两个字符之间的子字符串-python DataFrame - Extract substring between two characters - python DataFrame Python 抓取两个特定字符之间的子字符串 - Python grab substring between two specific characters 在由熊猫中两个特定字符分隔的列中提取子字符串 - Extract a substring in a column which is delimited by two specific characters in Pandas 如何从具有相同后缀的 python 中的 substring 中提取特定数量的字符 - How to extract specific number of characters from a substring in python with same suffix 去除子字符串中的特定字符 - Strip specific characters in a substring 在python中的2个字符串之间提取子字符串 - Extract substring between 2 strings in python 在“a”和“(”+2个未知字符+“)”之间找到substring - find substring between “a” and “(” + 2 unknown characters +“)” 从字符串中提取 substring 和 x 字符 - Extract a substring and x characters from a string 使用正则表达式在 python 中的 substring 之后提取字符 - Use regex to extract characters after a substring in python 如何为CSV文件中的每一列提取两个字符之间的子字符串,并将这些值复制到Python中的新列中? - How can I extract a substring between two characters for every row of a column in a CSV file and copy those values into a new column in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM