In Python, how would you write a regex that matches the following?
The string contains one or more "@" symbols. One or more of those symbols must have a chara开发者_如何学Ccter after it (not a space).
import re
my_regex = re.compile(r'@\S+')
The \S
class matches any non-whitespace character. If you'd prefer only alphanumeric characters, you might want to use \w
instead.
Then, if you wanted to get all of the instances where it matched:
for match in my_regex.finditer(string_to_search):
# Do something with the MatchObject in 'match'
More details on finditer
are available here: http://docs.python.org/library/re.html#re.finditer
import re
pattern = re.compile(r'@+\S+')
This pattern follows the question's spec more closely: "one or more "@" symbols"
精彩评论