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

时间:2014-06-12 00:34:17

标签: python regex

我在python中有一个字符串

text = '(b)'

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

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

在python中最好的方法是什么,

4 个答案:

答案 0 :(得分:3)

我不认为这里需要正则表达式。您可以使用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'
>>>

答案 1 :(得分:0)

不花哨,但这是非常通用的

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

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

答案 2 :(得分:0)

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']

答案 3 :(得分:0)

你可以通过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'
相关问题