user4929195
user4929195

Reputation:

Regex - Complex Capture

This is hard to explain, I have this Regex ([a-zA-Z][\w.]+|[0-9][0-9_.]*[a-zA-Z]+[\w.]*):(.*) - It captures Username:Whatever and it only captures usernames on the First Capture Group.

The issue is that when I replace the Username part with an Email, It captures everything after the @ of the email. I want it to capture NOTHING at all not even the "Whatever" bit.

I cant find a way to do this.

Example input's:

[email protected]:123abc
username:123abc
[email protected]:abc123
Shiny:abc123

Output should be:

username:123abc
Shiny:abc123

Current Regex outputs:

domain.ext:123abc
username:123abc
gmail.com:abc123
shiny:abc123

Upvotes: 0

Views: 36

Answers (1)

castletheperson
castletheperson

Reputation: 33496

Adding anchors ^ and $ to the beginning and end of the regex will force the matches to occur from the beginning to the end of the string.

^([a-zA-Z][\w.]+|[0-9][0-9_.]*[a-zA-Z]+[\w.]*):(.*)$

If you then activate the Multiline mode, it will make the anchors match at the beginning and end of each line instead.

Regex101 Demo

Upvotes: 1

Related Questions