This should be easy and this regex works fine to search for words beginning with specific characters, but I can't get it to match hashes and question marks.
This works and matches words beginning a:
r = re.compile(r"\b([a])(\w+)\b")
But these don't match: Tried:
r = re.compile(r"\b([#?])(\w+)\b")
r = re.compile(r"\b([\#\?])(\w+)\b")
r = re.compile( r"([#\?][\w]+)?")
even tried just matching hashes
r = re.compile( r"([#][\w]+)?"
r = re.compile( r"([/#][\w]+)?"
text = "this is one #tag and this is ?another tag"
items = r.findall(text)
expecting to get:
[('#', 'tag'), ('?', 'another')]
\b
matches the empty space between a \w
and \W
(or between a \W
and \w
) but there is no \b
before a #
or ?
.
In other words: remove the first word boundary.
Not:
r = re.compile(r"\b([#?])(\w+)\b")
but
r = re.compile(r"([#?])(\w+)\b")
you are using Python, regex is the last thing to come to mind
>>> text = "this is one #tag and this is ?another tag"
>>> for word in text.split():
... if word.startswith("#") or word.startswith("?"):
... print word
...
#tag
?another
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With