C# 将句子中的多个id提取到变量中
我将字符串作为输入C# 将句子中的多个id提取到变量中,c#,string,C#,String,我将字符串作为输入 The user with id '2156' helped the user '2156' to acquire an item with the id '1' with the quantity of '1'. 我想从字符串中获取用户id和项目id 我可以将输入字符串与常量字符串匹配并从输入中获取id吗 public const string StashAcquired = @"The user with id '{0}' helped the user '
The user with id '2156' helped the user '2156' to acquire an item with the id '1' with the quantity of '1'.
我想从字符串中获取用户id
和项目id
我可以将输入字符串与常量字符串匹配并从输入中获取id吗
public const string StashAcquired =
@"The user with id '{0}' helped the user '{1}' to acquire an item with the id '{2}' with the quantity of '{3}'.";
here: {0} is a `user id`
{2} is a `item id`
我不要其他号码,只要两个身份证
@"The user with id '{userid}' helped the user '2156' to acquire an item with the id '{itemid}' with the quantity of '1'.";
rest id与我无关:
我尝试从输入字符串中获取数字,并将其分配到一个变量中,如:
const string input = @"The user with id '2156' helped the user '2156' to acquire an item with the id '1' with the quantity of '1'.";
var number = GetNumbersFromString(input);
GetNumbersFromString
public int[] GetNumbersFromString(string str)
{
List<int> result = new List<int>();
string[] numbers = Regex.Split(str, @"\D+");
int i;
foreach (string value in numbers)
{
if (int.TryParse(value, out i))
{
result.Add(i);
}
}
return result.ToArray();
}
这种方法可能有效:使用“
作为delimeter拆分字符串。然后,用户id是字符串的第二部分,项目id是字符串的第六部分。这将产生以下代码:
string input = "The user with id '2156' helped the user '2156' to acquire an item with the id '1' with the quantity of '1'.";
string[] parts = input.Split('\'');
string rawUserId = parts[1];
string rawItemId = parts[5];
int userId = int.Parse(rawUserId);
int itemId = int.Parse(rawItemId);
在线演示:似乎关键字是“用户”加上“id”和“项目”加上“id”。正则表达式应该反映这一点,例如
user.*id.*'(?<userid>.*)'.*item.*id.*'(?<itemid>.*)'
不幸的是,这将添加一个新的组,这可能是您不想要的。让我们使用?:
使其成为非捕获:
user.*id.*'(?<userid>.*)'.*(?:item|module|course).*id.*'(?<itemid>.*)'
因为它在“user”和“id”后面有两个撇号。您希望*
不那么贪婪,所以将其更改为*?
:
user.*id.*?'(?<userid>.*?)'.*(?:item|module|course).*id.*'(?<itemid>.*)'
user.*id.*(?*?)。*(?:项目|模块|课程)。*id.*(?*)
另见
守则:
var s = "The user with id '2156' helped the user '2156' to acquire an item with the id '1' with the quantity of '1'";
var pattern = "user.*id.*?'(?<userid>.*?)'.*(?:item|module|course).*id.*'(?<itemid>.*)'";
var rex = new Regex(pattern);
var m = rex.Matches(s);
Console.WriteLine(m[0].Groups["userid"].Value + " " + m[0].Groups["itemid"].Value);
var s=“id为“2156”的用户帮助用户“2156”获取id为“1”且数量为“1”的物品;
var pattern=“user.*id.*?”(?*)。*(?:项目|模块|课程)。*id.*(?*)”;
var rex=新正则表达式(模式);
var m=重复匹配项;
Console.WriteLine(m[0]。组[“用户ID”].Value+“”+m[0]。组[“项目ID”].Value);
使用Regex时的专业提示:使用。它有一个非常强大的输出替代品,用于控制台。WriteLine()
。它可以执行以下操作:
这样,您就不需要自己编写所有代码。相反,您可以将对象看作一个表。希望这对您有所帮助,使用Regex:
public static int GetIDFromString(string text, string partName)
{
var matchUserPart = Regex.Match(text, @$"{partName}(.*?)id(.*?)\d+", RegexOptions.IgnoreCase).Value;
return matchUserPart.Length > 0 ? int.Parse(Regex.Replace(matchUserPart, @"\D", "")) : 0;
}
您可以这样使用它:
int userID = GetIDFromString(text, "user");
int itemID = GetIDFromString(text, "item");
请注意,Regex类位于System.Text.RegularExpressions命名空间中
在Regex中,\d(小写)用于每个数字,反之亦然\d(大写)表示Regex的模式中的每个非数字,(.*?中的标记使Regex匹配第一次出现(关闭regex中的贪婪)。问题的答案是“是的,你可以”。你需要实现它,它应该不会太难。除了声明字符串之外,你还尝试过其他方法吗?因此你知道输入的形式是“id为“{id1}”的用户帮助了用户“{id2}”(其余与此无关)“
。您可以分析它。您尝试过做什么?@ThomasWeller我已经更新了我的代码。@CodeCaster我需要的id是@”id为“{userid}”的用户帮助用户“2156”获取了id为“{itemid}”且数量为“1”的项目。”
rest other不相关感谢您提供了所有必要的信息。下次,请提前提供所有信息。这将减少您的问题吸引反对票的可能性。@AakashBashyal这应该没什么大不了的。只需查看您的EventName,了解如何解析字符串:switch(EventName){case“An Item acquiredstring();break;case“课程模块已查看”:ParseCourseModuleString();break;…
将上述解决方案转换为其他字符串格式应该不难。如果在case中我需要撇号中的所有内容
?如case:“id为“2253”的用户查看了课程模块id为“278”的“hvp”活动”"我需要所有三个…即userid
、activity
和moduleid
@AakashBashyal:请不要一点一点地扩展这个问题。你问了三件事:courseid、moduleid和itemid。现在你也需要activityid。我感觉下周我已经为你开发了一个完整的应用程序。让这个例子开始运行,理解它,做一些实验,试着自己解决下一个问题,如果不可能在接下来的3小时内解决,再问另一个问题。@AakashBashyal:这个答案应该可以帮助你完成额外的任务:user with id
短语在某些情况下有时会出现2次。在这种情况下怎么办?坚持使用regex。就像在这种情况下:<代码>“说明”:“id为'2184'的用户已查看id为'1900'的尝试,该尝试属于id为'2184'的用户,用于课程模块id为'228'的测验。”
已使用'(.*)'模式,该模式关闭正则表达式的渴望模式,使其与文本中的第一个匹配。它不会停留在那里。您可以帮助我了解该示例的工作代码吗?这是一个包含示例文本的工作代码
var s = "The user with id '2156' helped the user '2156' to acquire an item with the id '1' with the quantity of '1'";
var pattern = "user.*id.*?'(?<userid>.*?)'.*(?:item|module|course).*id.*'(?<itemid>.*)'";
var rex = new Regex(pattern);
var m = rex.Matches(s);
Console.WriteLine(m[0].Groups["userid"].Value + " " + m[0].Groups["itemid"].Value);
public static int GetIDFromString(string text, string partName)
{
var matchUserPart = Regex.Match(text, @$"{partName}(.*?)id(.*?)\d+", RegexOptions.IgnoreCase).Value;
return matchUserPart.Length > 0 ? int.Parse(Regex.Replace(matchUserPart, @"\D", "")) : 0;
}
int userID = GetIDFromString(text, "user");
int itemID = GetIDFromString(text, "item");