我有一些行文字。我希望从这个文本创建事件和意图活动,将它们放在列表上,然后将它们导入数据库。让我更具体一点。我们假设我有以下句子: “ 在Party1上,我们会喝点饮料,吃很好的食物,玩ps3游戏。在Party2上,我们将用嘻哈歌曲进行卡拉OK和跳舞。 “
我的愿望输出想成为:
活动1:Party1
Activitie1: take some drinks
Activitie2: eat very good food
Activitie3: play ps3 games
Event2:Party2
Activitie1: do karaoke
Activitie2: dance with hip hop songs
到目前为止,我所做的是允许特殊字符“#,*”。所以,当我有*我制作一个事件时,当我有#我产生一个活动。因此,我可以制作活动和活动,但我无法与之匹敌。我需要找到一种匹配(意图)生成元素的方法。我的代码:
string astring = Convert.ToString(lblrowtext.Text);
if (astring.IndexOf("#") >= 0 || astring.IndexOf("*") >= 0)
{
string[] Eventsarray;
string[] Activitiessarray;
List<string> listofEvents = new List<string>();
List<string> listofActivities = new List<string>();
Activitiessarray = Regex.Matches(astring, @"#([^\*#]*)").Cast<Match>()
.Select(m => m.Groups[1].Value).ToArray();
Eventsarray = Regex.Matches(astring, @"\*([^\*#]*)").Cast<Match>()
.Select(m => m.Groups[1].Value).ToArray();
if (Activitiessarray != null)
{
if (Activitiessarray.Count() > 0)
{
listofActivities = new List<string>(Activitiessarray);
ViewState["listofActivities"] = listofActivities;
Repeater1.DataSource = listofActivities;
Repeater1.DataBind();
}
if (Eventsarray != null)
{
if (Eventsarray.Count() > 0)
{
listofEvents = new List<string>(Eventsarray);
ViewState["listofEvents"] = listofEvents;
Repeater2.DataSource = listofEvents;
Repeater2.DataBind();
}
}
}
}
答案 0 :(得分:1)
您可以像下面这样实现:
private static void Main(string[] args)
{
var updationIndex = 0;
const string inputString = "On *Party1 #we will take some drinks, #eat very good food and #play ps3 games.# ";
Func<string, char, char, string> getMyString = (givenString, skipTill, takeTill) =>
{
var opString =
new string(
givenString.ToCharArray()
.SkipWhile(x => x != skipTill)
.Skip(1)
.TakeWhile(x => x != takeTill)
.ToArray());
updationIndex = inputString.IndexOf(givenString, StringComparison.CurrentCultureIgnoreCase)
+ opString.Length;
return opString;
};
var eventName = getMyString(inputString, '*', '#');
Console.WriteLine("Event" + eventName);
Console.WriteLine("Activities: ");
while (updationIndex < inputString.Length)
{
var activity = getMyString(inputString.Remove(0, updationIndex), '#', '#');
Console.WriteLine(activity);
if (string.IsNullOrWhiteSpace(activity))
{
break;
}
}
}
答案 1 :(得分:0)
创建一个您添加活动的Event类。每次遇到 * 时,都要创建一个新的Event实例。
伪C#:
class Event
{
string Description;
List<Activity> Activities;
}
class Activity
{
string Description;
}
List<Event> ParseEventLines(IEnumerable<string> allLines)
{
var result = new List<Event>();
Event currentEvent = null;
foreach (var line in allLines)
{
if (line.StartsWith("*"))
{
currentEvent = new Event { Description = line };
result.Add(currentEvent);
}
else if (line.StartsWith("#"))
{
currentEvent.Activities.Add(new Activity { Description = line });
}
}
return result;
}