James
James

Reputation: 157

Regex Capture Groups in C#

I'm trying to come up with a regex that will split out a string in 3 different parts: Name, Address, and Phone Number.

This is what I currently have:

^(?<Name>\w.*)\s+(?<Address>\d+\s\w+.*\d{5,9})\s+(?<Phone>\d+.*)

Here is what regex101 is giving me back with the following sample string: enter image description here

But if I add in a '#' before the suite number, it works as planned: enter image description here

I've even tried running just the address regex against the entire string in a separate instance and it worked just fine. There's something going on with the capture groups that's throwing it off. Any suggestions would be greatly appreciated!

Upvotes: 0

Views: 88

Answers (1)

BoltBait
BoltBait

Reputation: 11489

Try making the name non-greedy:

^(?<Name>\w.*?)\s+(?<Address>\d+\s\w+.*\d{5,9})\s+(?<Phone>\d+.*)

Upvotes: 1

Related Questions