简体   繁体   English

从字符串中提取多个值

[英]Extract multiple values from a string

I need to extract values from a string. 我需要从字符串中提取值。

string sTemplate = "Hi [FirstName], how are you and [FriendName]?"

Values I need returned: 我需要返回的值:

  • FirstName 名字
  • FriendName 朋友名

Any ideas on how to do this? 有关如何执行此操作的任何想法?

You can use the following regex globally: 您可以全局使用以下正则表达式

\[(.*?)\]

Explanation: 说明:

\[ : [ 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.

Example: 例:

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

If the format/structure of the text won't be changing at all, and assuming the square brackets were used as markers for the variable, you could try something like this: 如果文本的格式/结构完全不变,并且假设方括号用作变量的标记,则可以尝试以下操作:

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;

You will need to tokenize the text and then extract the terms. 您将需要标记文本,然后提取术语。

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

Explanation: 说明:
You tokenize the terms, which separates the string into a string array with each element being the char sequence between each delimiter, in this case between spaces which is the words. 您可以对术语进行标记化,从而将字符串分成字符串数组,每个元素都是每个定界符之间的char序列,在这种情况下,就是两个单词之间的空格。 From this word array we know that we want the 3rd word (element) and the 7th word (element). 从这个单词数组中我们知道我们想要第三个单词(元素)和第七个单词(元素)。 However each of these terms have punctuation at the end. 但是,每个术语的末尾都有标点符号。 So we convert the strings to a char array then back to a string minus that last character, which is the punctuation. 因此,我们将字符串转换为char数组,然后再转换为减去最后一个字符(标点符号)的字符串。

Note: 注意:
This method assumes that since it is a first name, there will only be one string, as well with the friend name. 此方法假定由于它是名字,所以只有一个字符串以及朋友名称。 By this I mean if the name is just Will, it will work. 我的意思是,如果名称只是Will,它将起作用。 But if one of the names is Will Fisher (first and last name), then this will not work. 但是,如果其中一个名字是Will Fisher(名字和姓氏),那么它将不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM