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

时间:2017-04-24 07:16:28

标签: python regex

我需要正则表达式匹配不以#

开头的单词

示例:

$customerData = Customer::where('cs_id','d0402be5-e1ba-4cb2-a80c-5340b406e2c3')->first();

dd($customerData['cs_id']);

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

#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

我该怎么做?

感谢您的帮助

3 个答案:

答案 0 :(得分:3)

许多方式,其中之一:

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

答案 1 :(得分:1)

没有正则表达式:

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

答案 2 :(得分:1)

试一试:

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

我认为这很有用。