【发布时间】:2014-08-03 03:10:22
【问题描述】:
我的代码应该接受一个包含多行的输入字符串,然后将该字符串的元素添加到一个新列表中。
示例输入如下所示:
[ (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]);
是否有人对我如何解决此问题并使其输出我想要的内容有任何建议?
【问题讨论】:
-
你只输出IntermediateList中的前4个元素,对应第一个元素。输出IntermediateList中的所有元素怎么样?
-
哈哈哈哈。我是一个白痴。谢谢,埃尔贡佐。
-
我觉得你不是白痴。 (当然,您的思维失误可能会有点令人尴尬……)必须承认您为正确解释输入、预期+观察到的输出和源代码问题所做的努力——所有这些都正确格式化。在本网站上提问的其他人可能会将您的问题作为“如何正确提问?” 的一个很好的例子。这绝对是你不是白痴的证据;)
-
好吧,谢谢你这么说。 ;)
-
你也应该用 "\((.*?)\)" 改变你的正则表达式,因为括号,你得到了双重结果