获得第一个字一个特殊字符后,Python中的字符串

问题描述 投票:4回答:3

我有一些字符串是这样的:

s='@VirginAmerica it was amazing, and arrived an hour early.'
t='heyyyyy@VirginAmerica , am I dreaming?'
m='heyyyyy @VirginAmerica , am I dreaming?'
u=''
f='@United...'
h='@United@VirginAmerica'

我要拿出后@字在数据帧并删除它的数据帧这个词。现在我用这一个,取出第一个字后,@但结果并不适用于所有的字符串是正确的。

s.split(' ', 1)[0]==>correct==>VirginAmerica
t.split(' ', 1)[0]==>wrong==>heyyyyy@VirginAmerica==>'VirginAmerica' is correct 
m.split(' ', 1)[0]==>correct==>VirginAmerica
u.split(' ', 1)[0]==>correct==>''
f.split(' ', 1)[0]==>wrong==>@United...==>'United' is correct
h.split(' ', 1)[0]==>wrong==>@United@VirginAmerica==>I just want the first one

和后@移除单词和无@和字写入整个字符串,我会收到这些:

s.split(' ', 1)[1]==>correct==>it was amazing, and arrived an hour early.
t.split(' ', 1)[1]==>wrong==>@VirginAmerica , am I dreaming?==>'heyyyyy , am I dreaming?' is correct 
m.split(' ', 1)[1]==>wrong==>@VirginAmerica , am I dreaming?==>VirginAmerica==>'heyyyyy , am I dreaming?' is correct
u.split(' ', 1)[1]==>wrong==>IndexError: list index out of range==> ''is correct
f.split(' ', 1)[1]==>wrong==>IndexError: list index out of range==>'...' is correct
h.split(' ', 1)[1]==>wrong==>IndexError: list index out of range==>'@VirginAmerica' is correct

你能帮我解决这个问题?这将是最好不要使用任何库。但如果它是唯一的选择,这是确定。

谢谢

python python-3.x dataframe
3个回答
1
投票

这里是一个测试你的代码

import re

s='@VirginAmerica it was amazing, and arrived an hour early.'
t='heyyyyy@VirginAmerica , am I dreaming?'
m='heyyyyy @VirginAmerica , am I dreaming?'
u=''
f='@United...'
h='@United@VirginAmerica'

def find_match(str):
  res = re.search('@(\w+)', str)
  if not res:
    return ''
  return res.group(1)

def sub_match(str):
  return re.sub('^[^@]*@\w+', '', str)

assert find_match(s) == 'VirginAmerica'
assert find_match(t) == 'VirginAmerica'
assert find_match(m) == 'VirginAmerica'
assert find_match(u) == ''
assert find_match(f) == 'United'
assert find_match(h) == 'United'

assert sub_match(s) == ' it was amazing, and arrived an hour early.'
assert sub_match(t) == ' , am I dreaming?'
assert sub_match(m) == ' , am I dreaming?'
assert sub_match(u) == ''
assert sub_match(f) == '...'
assert sub_match(h) == '@VirginAmerica'

find_match(STR)

其核心思想是使用正则表达式。

我们正在寻找与@号开始的第一个字。这很容易与旁边的正则表达式来形容

=> Qazxswpoi

其中@\w+表示匹配精确的字符和@匹配1个或多个字characters\w+

同时,我们也使用(docs explanation for this)挑结果组,因为我们感兴趣的是一个字不()我们只换@

=> Qazxswpoi

sub_match(STR)

它使用正规表达式相同的想法,但因为与\w+字符的第一场比赛的情况下的一个小一点小技巧。

为此目的,第一,我们匹配这不是@(\w+)所有字符 - > @正则表达式的一部分,然后我们使用我们在@但没有组使用相同的正则表达式,因为我们只需要[^@]*更换整个事情。

PS链接,启动在网络find_match(str)代码在这里,你可以测试@


2
投票

使用正则表达式另一种实现,这得到以下从字符串https://repl.it/repls/SinfulWhichSynergy字。

your python regexps and practice with

这种打印

@

此外,它是很好的注意,import re s='@VirginAmerica it was amazing, and arrived an hour early.' t='heyyyyy@VirginAmerica , am I dreaming?' m='heyyyyy @VirginAmerica , am I dreaming?' u='' f='@United...' h='@United@VirginAmerica' for text in [s, t, m, u, f, h]: print(re.findall(r'@(\w+)', text)) 是一个标准的Python库这样你就不会使用未包括在Python已经什么。

如果你不希望使用正则表达式,你可以使用拆分,但仍然像这样将导致与上述相同:

['VirginAmerica ']
['VirginAmerica ']
['VirginAmerica ']
[]
['United']
['United', 'VirginAmerica']

编辑

根据你的评论,得到了这个词第一次出现以下re例如s='@VirginAmerica it was amazing, and arrived an hour early.' t='heyyyyy@VirginAmerica , am I dreaming?' m='heyyyyy @VirginAmerica , am I dreaming?' u='' f='@United...' h='@United@VirginAmerica' for text in [s, t, m, u, f, h]: _, *words = text.split('@') print([words.split()[0] for word in words]) 只需使用列表分片的第一个字@(只是一定要确保至少有一个相匹配的正则表达式以其他方式使用除块一试字)

'united'

得到了这个词,而不@word使用h第一次出现的我还包括一个空格和问号所以在打印时它看起来右后删除空间。 (如果您希望它打印的所有OCCURENCES的去除刚刚从这种方法去除h='@United@VirginAmerica' re.sub(r'@(\w+)' h)[0] #United

sub

我拥有的一切我在repl.it count


0
投票

这个答案只是使用简单的Python的功能,并试图不被“Python化”,因为这可以为初学者混淆。

基本上它寻找句子中的一个与s='@VirginAmerica it was amazing, and arrived an hour early.' re.sub(r'@(\w+) ?', '', s, count=1) #it was amazing, and arrived an hour early. give it a try,它返回的第一次出现的位置索引“@”,或@。一个在OP的问题没有提及的事情,是什么构成了“@word” - 在它停止更重要。我加入了sentence.find('@')不断容纳所有代表一个单词的末尾字母。因此,功能从第一-1和高达WORD_END的第一个字符是“@word”。

另一点需要注意的是,有没有那里是测试用例没有“@”中的句子。

@

给出的结果:

WORD_END
© www.soinside.com 2019 - 2024. All rights reserved.