To find mentions in a string using regex in Python, you can use the re
module. A mention starts with the @
symbol and is followed by a string of alphanumeric characters (with underscores). Here's the code that will find all mentions in a given string:
main.py200 chars13 lines
Output:
main.py19 chars2 lines
Explanation:
re
module which provides support for regular expressions in Python.@\w+
.
@
matches the @
symbol.\w
matches any alphanumeric character or underscore.+
indicates that there must be at least one character after the @
.findall()
method to find all occurrences of the regex pattern in the input string.gistlibby LogSnag