python - Matching an optional '#' does not seem to be working properly -



python - Matching an optional '#' does not seem to be working properly -

i'm attempting total words or hashtags string, seems though i'm applying 'optional character' ? flag wrong in regex.

here code:

print re.findall(r'(#)?\w*', text) print re.findall(r'[#]?\w*', text)

thus 'this sentence talking this, #this, #that, #etc'

should homecoming matches 'this' , '#this'

yet seems returning list empty strings other random things.

what wrong regex?

edit:

i'm attempting whole spam words, , seem have jumbled myself...

s = 'spamword' print re.findall(r'(#)?'+s, text)

i need match whole word, , not word parts...

you can utilize word boundary in regex:

s = 'spamword' re.findall(r'#?' + s + r'\b', text)

python regex

Comments

Popular posts from this blog

java Multi query from Mysql using netbeans -

c# - DotNetZip fails with "stream does not support seek operations" -

c++ - StartServiceCtrlDispatcher don't can access 1063 error -