Reputation: 69
VS 2015, c#. I have a string...
string str = "Name;IPAddress";
I want to extract just the IPAddress. I suspect Regex is the best way to do it but I am unsure.
Any assistance greatly appreciated.
Upvotes: 2
Views: 753
Reputation: 133
Why do you think Regex is the best way? Do you also want to validate name and IP address?
string sInput = "John;127.0.0.1";
string[] arrNameAndIP = sInput.Split(';');
bool bIsInputValid = false;
if(arrNameAndIP.Length == 2)
{
Regex rgxNamePattern = new Regex("^[A-za-z]+$");
bool bIsNameValid = rgxNamePattern.IsMatch(arrNameAndIP[0]);
IPAddress ipAddress;
bool bIsIPValid = IPAddress.TryParse(arrNameAndIP[1], out ipAddress);
bIsInputValid = bIsNameValid && bIsIPValid;
}
Upvotes: 1