简体   繁体   中英

C# Add elements of Array to List within a Loop

My code should take in an input string with multiple lines, and then add the elements of that string 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 am hoping that my code would return a List for an individual line, with all the elements included. However, my current output only captures the first element of each line. Like so:

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 resemble 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 as to how I can fix this, and make it output what I want?

This is a classic non-greedy RegEx. There are many ways to do it (perhaps better), but the following will do your job (note the non-greedy syntax for the pattern):

    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());
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM