Home > Software design >  regex - get two string from this string: [(10.3.4.5/001_3CX)]
regex - get two string from this string: [(10.3.4.5/001_3CX)]

Time:11-26

I have a string: [(10.3.4.5:83/001_3CX/asd_43)]

From this string I want to get two strings:

10.3.4.5:83

001_3CX/asd_43

What is the best solution for this? I'm coding in c#.

CodePudding user response:

RegEx appraoch https://dotnetfiddle.net/7ZJhUL

string input = "[(10.3.4.5:83/001_3CX/asd_43)]";
Match result = Regex.Match(input, @"\[\((.*?)\/(.*)\)\]");
string first = result.Groups[1].Value; //10.3.4.5:83
string second = result.Groups[2].Value; //001_3CX/asd_43

CodePudding user response:

string test = "[(10.3.4.5:83/001_3CX/asd_43)]";
test.replace_string_by_character("[(","");
test.replace_string_by_character(")]","");
// (current result) test = "10.3.4.5:83/001_3CX/asd_43"
int index = locate_character_inside_string("/",test);

string first_to_return = substring_basedon_index(test,0,index - 1);
string second_to_return = substring_basedon_index(test,index   1, length(test));
// (result) first_to_return = "10.3.4.5:83";
// (result) second_to_return = "001_3CX/asd_43";

There are plenty of internet posts on the exact names of the functions and methods I've mentioned :-)

  • Related