[英]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 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.