Shiny Shiny - 4 months ago 14
Vb.net Question

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:

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


Output should be:

username:123abc
Shiny:abc123


Current Regex outputs:

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

Answer

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