I'm trying to match a "#" followed by letters if and only if it's preceded by newline, whitespace or is the first character in a string. The first two I've done, but I'm having a hard time matching if it's the first character in a string. I'm trying to find a use for '\A', but it doesn't work to just add it to the class containing newline and whitespace. What have I missed?
The regular expression I've come up with so far is:
from re import findall, escape
from string import punctuation, whitespace
NEWLINE = """\r\n?|\n"""
INVALID_TAG_CHARACTERS = escape(punctuation.replace('-', '').replace('_', '') + whitespace)
VALID_TAGS = r'[\s%s]+#[^%s]+' % (NEWLINE, INVALID_TAG_CHARACTERS)
tags = findall(VALID_TAGS, text)