匹配不以开头的单词的正则表达式#

2024-04-19 06:38:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要正则表达式来匹配不以#开头的单词

示例:

#Repost @edbyellen
 #EDEllenDeGeneres rugs so cozy you can walk on them, nap, on them, sleep on them you get the picture
Now available in select retailers in the US, crafted by @LoloiRugs. #EDbyLoloi

我想要一个正则表达式来匹配这样的词:

@edbyellen
rugs so cozy you can walk on them, nap, on them, sleep on them you get the picture
Now available in select retailers in the US, crafted by @LoloiRugs.

我该怎么做?你知道吗

谢谢你帮我


Tags: theinyougetsoonsleepcan
3条回答

试试看:

\B([a-zA-Z]+\b)(?!;)

我认为这很有用。你知道吗

不带正则表达式:

for line in lines:
  for word in line.split():
    if not word.startswith('#'):
      print(word)
  print()

很多方法,其中之一:

import re

text = """#Repost @edbyellen
 #EDEllenDeGeneres rugs so cozy you can walk on them, nap, on them, sleep on them you get the picture
Now available in select retailers in the US, crafted by @LoloiRugs. #EDbyLoloi"""

print re.sub(r'#[^# ]+', '', text)

输出:

 @edbyellen
  rugs so cozy you can walk on them, nap, on them, sleep on them you get the picture
Now available in select retailers in the US, crafted by @LoloiRugs. 

来自Yoav Glazner的反馈,查看匹配字符串:

print re.sub(r'#[^# ]+', '', text).split()

输出:

['@edbyellen', 'rugs', 'so', 'cozy', 'you', 'can', 'walk', 'on', 'them,', 'nap,', 'on', 'them,', 'sleep', 'on', 'them', 'you', 'get', 'the', 'picture', 'Now', 'available', 'in', 'select', 'retailers', 'in', 'the', 'US,', 'crafted', 'by', '@LoloiRugs.']

相关问题 更多 >