Spindel
Spindel

Reputation: 289

Regex - Find text after @ and before specific chars

I have this code:

replies = re.findall(r'@([^\\s]+)', string_content_here)

What I am trying to do is to find all usernames after @ sign. I only want to find @ strings before space, comma, exclamation mark and question mark.

Examples:

@Spindel <--- Should return Spindel
@Spindel, <--- Should return Spindel
@Spindel! <--- Should return Spindel
@Spindel? <--- Should return Spindel
@gmail.com <--- I don't want this at all

Upvotes: 1

Views: 82

Answers (2)

John Zwinck
John Zwinck

Reputation: 249183

r'@([\w]+)(?:$|[ ,!?])'

The ?: is there to make the second group "non-capturing", so you get only the names you want in the results.

Upvotes: 2

Zaiborg
Zaiborg

Reputation: 2522

you can use this, its not perfect but should do the trick

replies = re.findall("@(\\w+)[ ,!?]", string_content_here)

Upvotes: 0

Related Questions