繁体   English   中英

从python中的字符串中提取字母数字子字符串

[英]Extracting alphanumeric substring from a string in python

我在python中有一个字符串

text = '(b)'

我想提取'b'。 我可以删除字符串的第一个和最后一个字母,但我不这样做的原因是因为文本字符串可能包含'(a)',(iii),'i)','(1'或'(2) '。有时它们根本没有括号。但它们总是包含一个字母数字值。但我同样想要检索那里的字母数字值。

这个专长必须在一行代码或代码块中完成,这些代码或代码块只返回值,因为它将在多种情况下迭代使用

什么是python中最好的方法,

我不认为这里需要正则表达式。 你可以用str.strip剥去任何括号:

>>> text = '(b)'
>>> text.strip('()')
'b'
>>> text = '(iii)'
>>> text.strip('()')
'iii'
>>> text = 'i)'
>>> text.strip('()')
'i'
>>> text = '(1'
>>> text.strip('()')
'1'
>>> text = '(2)'
>>> text.strip('()')
'2'
>>> text = 'a'
>>> text.strip('()')
'a'
>>>

关于@MikeMcKerns的评论,一个更强大的解决方案是将string.punctuation传递给str.strip

>>> from string import punctuation
>>> punctuation  # Just to demonstrate
'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
>>>
>>> text = '*(ab2**)'
>>> text.strip(punctuation)
'ab2'
>>>

不花哨,但这很通用

>>> import string
>>> ''.join(i for i in text if i in string.ascii_letters+'0123456789')

这适用于字符串中间的所有种类的括号组合,以及如果您有其他非字母数字字符(括号内)。

re.match(r'\(?([a-zA-Z0-9]+)', text).group(1)

由exmple提供的输入将是:

>>> a=['(a)', '(iii)', 'i)', '(1' , '(2)']
>>> [ re.match(r'\(?([a-zA-Z0-9]+)', text).group(1) for text in a ]
['a', 'iii', 'i', '1', '2']

你可以通过python的re模块来做到这一点,

>>> import re
>>> text = '(5a)'
>>> match = re.search(r'\(?([0-9A-Za-z]+)\)?', text)
>>> match.group(1)
'5a'
>>> text = '*(ab2**)'
>>> match = re.search(r'\(?([0-9A-Za-z]+)\)?', text)
>>> match.group(1)
'ab2'

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM