C # Add array elements to a list in a loop

My code should take an input string with multiple lines, and then add the elements of that line to a new list.

An example input would look like this:

[ (Nucleosome, Stable, 21, 25), (Transcription_Factor, REB1, 48, 6), (Nucleosome, Stable, 64, 25), (Transcription_Factor, TBP, 90, 5) ]
[ (Transcription_Factor, MCM1, 2, 8), (Nucleosome, Stable, 21, 25), (Transcription_Factor, REB1, 48, 6), (Nucleosome, Stable, 64, 25) ] 

I hope that my code will return a list for a single line with all elements included. However, my current output only captures the first element of each row. For instance:

Found type: 'Nucleosome', Found subtype: 'Stable', Found position: '21', Found length '25'
Found type: 'Transcription_Factor', Found subtype: 'MCM1', Found position: '2', Found length '8'

Ideally, the output would look like this:

Found type: 'Nucleosome', Found subtype: 'Stable', Found position: '21', Found length '25'
Found type: 'Transcription_Factor', Found subtype: 'REB1', Found position: '48', Found length '6'
Found type: 'Nucleosome', Found subtype: 'Stable', Found position: '64', Found length '25'
Found type: 'Transcription_Factor', Found subtype: 'TBP', Found position: '90', Found length '5'

Here is my current code:

public static void read_time_step(string input)
{
    string pattern = @"\(((.*?))\)";
    string intermediateString1 = "";
    string[] IntermediateArray = (intermediateString1).Split (new Char[] {' '});
    List<string> IntermediateList;

    IntermediateList = new List<string> ();

    foreach(Match match in Regex.Matches(input, pattern, RegexOptions.IgnoreCase))
        {
            intermediateString1 = Regex.Replace(match.Value, "[.,()]?", "");

            IntermediateArray = (intermediateString1).Split (new Char[] {' '});
            IntermediateList.AddRange (IntermediateArray);
        }

    Console.WriteLine("Found type: '{0}', Found subtype: '{1}', Found position: '{2}', Found length '{3}'", IntermediateList[0], IntermediateList[1], IntermediateList[2], IntermediateList[3]);

Does anyone have any suggestions on how I can fix this and conclude what I want?

+4
source share
1 answer

- RegEx. (, ), ( ):

    static void Main(string[] args)
    {
        string input = "[ (Nucleosome, Stable, 21, 25), (Transcription_Factor, REB1, 48, 6), (Nucleosome, Stable, 64, 25), (Transcription_Factor, TBP, 90, 5) ]";
        read_time_step(input);

        Console.Read();
    }

    public static void read_time_step(string input)
    {
        string pattern = @"\((.)*?\)";

        MatchCollection mc = Regex.Matches(input, pattern, RegexOptions.IgnoreCase);
        foreach (Match match in mc)
        {
            string v = match.Value.Trim('(', ')');

            string[] IntermediateList = v.Split(',');

            Console.WriteLine("Found type: '{0}', Found subtype: '{1}', Found position: '{2}', Found length '{3}'", 
            IntermediateList[0].Trim(), IntermediateList[1].Trim(), IntermediateList[2].Trim(), IntermediateList[3].Trim());
        }
    }
0

Source: https://habr.com/ru/post/1544368/


All Articles