Python,删除单词以特定字符开头

时间:2014-11-28 20:55:10

标签: python

如何在python中删除以特定字符开头的单词?

e.g ..

string = 'Hello all please help #me'

我想删除以#

开头的单词

我想要的结果是:

Hello all please help 

5 个答案:

答案 0 :(得分:3)

这里使用split的一个问题是它删除了空格。例如,

In [114]: 'a  b \tc\nd'.split()
Out[114]: ['a', 'b', 'c', 'd']

因此,再次将' '.join 重新加入原始字符串:

In [115]: ' '.join('a  b \tc\nd'.split())
Out[115]: 'a b c d'

如果您想保留原始字符串并删除以#开头的字词,那么您可以使用正则表达式:

In [119]: import re

In [120]: re.sub(r'(\s)#\w+', r'\1', 'Hello all please help #me   but#notme')
Out[120]: 'Hello all please help    but#notme'

<强>解释

https://regex101.com有一个方便的工具来帮助您理解正则表达式。 例如,"(\s)#\w+"表示1st Capturing group (\s) \s match any white space character [\r\n\t\f ] # matches the character # literally \w+ match any word character [a-zA-Z0-9_] Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy] 的含义:

' #me'

由于此正则表达式模式以匹配空格开头,'but#notme'匹配,但re.sub不匹配。

r'\1'的第二个参数\1是替换模式。 re.sub 告诉' #me'将匹配替换为第一个捕获组。所以 匹配' '已替换为空格{{1}}。

答案 1 :(得分:3)

使用最明显的解决方案:

txt = 'Hello all please help #me'
# better to not use 'string' as variable name

' '.join(word for word in txt.split(' ') if not word.startswith('#'))

请注意,在这种情况下,最好使用带有显式空格的split(' ')作为分隔符,而不是更常见的无参数split()。这样你就不会失去换行符或多个空格。

答案 2 :(得分:2)

>>> a = "Hello all please help #me "
>>> filter(lambda x:x[0]!='#', a.split())
['Hello', 'all', 'please', 'help']

你可以使用空格加入它:

>>> " ".join(filter(lambda x:x[0]!='#', a.split()))
'Hello all please help'

让我一步一步解释你:

>>> a = "Hello all please help #me "
>>> a.split()                          # split, splits the string on delimiter, by default its whitespace
['Hello', 'all', 'please', 'help', '#me']
>>> >>> filter(lambda x:x[0]!='#', a.split())
['Hello', 'all', 'please', 'help']

filter仅返回条件为True的元素。

答案 3 :(得分:1)

我会做这样的事情:

' '.join(word for word in "help #me please".split() if word[0]!='#')

答案 4 :(得分:0)

作为unutbu's answer的补充,用于捕捉句子开头的事件

> re.sub(r'(\s)#\w+', r'\1', '#Hello all please help #me   but#notme')
 '#Hello all please help    but#notme'

> re.sub(r'(\s)#\w+', r'\1', '#Hello all please help #me   but#notme')
 'all please help    but#notme'
  

没有足够的代表发表评论

相关问题