C#循环将数组元素添加到列表中

4

我的代码应该接收一个有多行的输入字符串,然后将该字符串的元素添加到一个新的列表中。

一个示例输入可能如下所示:

[ (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) ] 

我希望我的代码能够返回一个包含每行所有元素的列表。 然而,我的当前输出只捕获了每行的第一个元素。 像这样:

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'

理想情况下,输出结果应该像这样:
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'

这是我的当前代码:

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]);

有没有人对我能如何解决这个问题并输出我想要的结果有任何建议?


1
你刚刚输出了IntermediateList中的前4个元素,这些元素对应第一个元素。那么输出IntermediateList中的所有元素呢? - user2819245
哈哈哈哈哈,我是个白痴。谢谢,elgonzo。 - user3731694
7
我认为你不是白痴。(当然,你的一时疏忽可能有点尴尬......)必须承认你为了正确解释你的问题而付出的努力,如提供输入、期望和观察输出以及源代码,并且所有这些都被适当地格式化。在这个网站上提问的其他人可以把你的问题作为一个好的例子来学习“如何恰当地提问”。这绝对证明了你不是白痴 ;) - user2819245
好的,谢谢你这么说。;) - user3731694
你还应该将你的正则表达式改为"\((.*?)\)",因为括号导致了双重结果。 - Bastianon Massimo
我同意@elgonzo的观点。为一个合适的问题而欢呼,我们都会犯错。不要太严厉了。 - Cubicle.Jockey
1个回答

0

这是一个经典的非贪婪正则表达式。有很多方法可以做到这一点(也许更好),但以下内容将完成您的工作(请注意模式的非贪婪语法):

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接