0

I have a file with the following content

aaaaa(fasdfiojasdlfkj)
213.df(fasdfsadffdfsd)
53434534535(oipowerier)
2.3.*12.4(asdfrwer)

i would like to have a list like this eventually,

List<string[]> sList = new List<string[]>();
sList[0] = new string[]{"aaaaa", "fasdfiojasdlfkj"};
sList[1] = new string[]{"213.df", "fasdfsadffdfsd"};
sList[2] = new string[]{"53434534535", "oipowerier"};
sList[3] = new string[]{"2.3.*12.4", "asdfrwer"};

3 Answers 3

3

You can do this without regex:

var result = stringlist.ConvertAll(x =>x.Split(new char[] {'(',')'},
                           StringSplitOptions.RemoveEmptyEntries));
Sign up to request clarification or add additional context in comments.

Comments

3

You don't need Regex for this - string.Split will be enough.

If you use it per line:

List<string[]> sList = new List<string[]>();
foreach(var line in fileLines)
{
    sList.Add(line.Split(new Char[]{ '(', ')'}, 
              StringSplitOptions.RemoveEmptyEntries));
}

Comments

0
List<string[]> sList = new List<string[]>();

MatchCollection matches = Regex.Matches(yourtext, @"([^\(]+)\(([^\)]+)\)");

foreach(Match mymatches in matches)
{

    //get the data
    string firststring = mymatches.Groups[1].Value;
    string secondstring = mymatches.Groups[2].Value;

    sList.Add(new string[] {firststring, secondstring});
}

not tested though....

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.