从字符串中提取多个值
Extract multiple values from a string
我需要从字符串中提取值。
string sTemplate = "Hi [FirstName], how are you and [FriendName]?"
我需要返回的值:
- 名字
- 好友名
关于如何做到这一点有什么想法吗?
您可以全局使用以下 regex:
\[(.*?)\]
解释:
\[ : [ is a meta char and needs to be escaped if you want to match it literally.
(.*?) : match everything in a non-greedy way and capture it.
\] : ] is a meta char and needs to be escaped if you want to match it literally.
示例:
string input = "Hi [FirstName], how are you and [FriendName]?";
string pattern = @"\[(.*?)\]";
Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase);
MatchCollection matches = rgx.Matches(input);
if (matches.Count > 0)
{
Console.WriteLine("{0} ({1} matches):", input, matches.Count);
foreach (Match match in matches)
Console.WriteLine(" " + match.Value);
}
您需要对文本进行标记,然后提取术语。
string[] tokenizedTerms = new string[7];
char delimiter = ' ';
tokenizedTerms = sTemplate.Split(delimiter);
firstName = tokenizedTerms[1];
friendName = tokenizedTerms[6];
char[] firstNameChars = firstName.ToCharArray();
firstName = new String(firstNameChars, 0, firstNameChars.length - 1);
char[] friendNameChars = lastName.ToCharArray();
friendName = new String(friendNameChars, 0, friendNameChars.length - 1);
说明:
您将术语标记化,将字符串分隔成一个字符串数组,每个元素是每个分隔符之间的字符序列,在本例中是空格之间的单词。从这个单词数组中我们知道我们想要第 3 个单词(元素)和第 7 个单词(元素)。但是,这些术语中的每一个在末尾都有标点符号。因此,我们将字符串转换为 char 数组,然后再转换回减去最后一个字符(标点符号)的字符串。
注:
此方法假定因为它是名字,所以只有一个字符串,还有朋友的名字。我的意思是,如果名字只是 Will,它就可以工作。但是如果其中一个名字是 Will Fisher(名字和姓氏),那么这将不起作用。
如果文本的 format/structure 根本不会改变,并且假设方括号用作变量的标记,您可以尝试这样的操作:
string sTemplate = "Hi FirstName, how are you and FriendName?"
// Split the string into two parts. Before and after the comma.
string[] clauses = sTemplate.Split(',');
// Grab the last word in each part.
string[] names = new string[]
{
clauses[0].Split(' ').Last(), // Using LINQ for .Last()
clauses[1].Split(' ').Last().TrimEnd('?')
};
return names;
我需要从字符串中提取值。
string sTemplate = "Hi [FirstName], how are you and [FriendName]?"
我需要返回的值:
- 名字
- 好友名
关于如何做到这一点有什么想法吗?
您可以全局使用以下 regex:
\[(.*?)\]
解释:
\[ : [ is a meta char and needs to be escaped if you want to match it literally.
(.*?) : match everything in a non-greedy way and capture it.
\] : ] is a meta char and needs to be escaped if you want to match it literally.
示例:
string input = "Hi [FirstName], how are you and [FriendName]?";
string pattern = @"\[(.*?)\]";
Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase);
MatchCollection matches = rgx.Matches(input);
if (matches.Count > 0)
{
Console.WriteLine("{0} ({1} matches):", input, matches.Count);
foreach (Match match in matches)
Console.WriteLine(" " + match.Value);
}
您需要对文本进行标记,然后提取术语。
string[] tokenizedTerms = new string[7];
char delimiter = ' ';
tokenizedTerms = sTemplate.Split(delimiter);
firstName = tokenizedTerms[1];
friendName = tokenizedTerms[6];
char[] firstNameChars = firstName.ToCharArray();
firstName = new String(firstNameChars, 0, firstNameChars.length - 1);
char[] friendNameChars = lastName.ToCharArray();
friendName = new String(friendNameChars, 0, friendNameChars.length - 1);
说明:
您将术语标记化,将字符串分隔成一个字符串数组,每个元素是每个分隔符之间的字符序列,在本例中是空格之间的单词。从这个单词数组中我们知道我们想要第 3 个单词(元素)和第 7 个单词(元素)。但是,这些术语中的每一个在末尾都有标点符号。因此,我们将字符串转换为 char 数组,然后再转换回减去最后一个字符(标点符号)的字符串。
注:
此方法假定因为它是名字,所以只有一个字符串,还有朋友的名字。我的意思是,如果名字只是 Will,它就可以工作。但是如果其中一个名字是 Will Fisher(名字和姓氏),那么这将不起作用。
如果文本的 format/structure 根本不会改变,并且假设方括号用作变量的标记,您可以尝试这样的操作:
string sTemplate = "Hi FirstName, how are you and FriendName?"
// Split the string into two parts. Before and after the comma.
string[] clauses = sTemplate.Split(',');
// Grab the last word in each part.
string[] names = new string[]
{
clauses[0].Split(' ').Last(), // Using LINQ for .Last()
clauses[1].Split(' ').Last().TrimEnd('?')
};
return names;