VS2008 C#:正则表达式和识别某些单词

VS2008 C#:正则表达式和识别某些单词,c#,regex,C#,Regex,我想使用正则表达式来识别字符串中的某些单词 例如: "bla bla bla | First Name = John Doe | City = Denver | bla bla bla | State = CA | bla bla bla" 在上面的字符串(用单词分隔)中,我想解析出First Name、City和State的内容,并将它们存储在哈希表中 我该怎么做呢?我认为最好的方法是使用正则表达式。我会使用string.Split(“|”)和string.IndexOf(“=”)来解析

我想使用正则表达式来识别字符串中的某些单词

例如:

"bla bla bla |   First Name = John Doe | City = Denver | bla bla bla | State = CA | bla bla bla"
在上面的字符串(用单词分隔)中,我想解析出First Name、City和State的内容,并将它们存储在哈希表中


我该怎么做呢?我认为最好的方法是使用正则表达式。

我会使用string.Split(“|”)和string.IndexOf(“=”)来解析元素。它肯定比正则表达式更简单。

我会使用string.Split(“|”)和string.IndexOf(“=”)来解析元素。它肯定比正则表达式更简单。

如果数据一致(即始终使用|和=作为分隔符),则可以使用字符串拆分方法将结果放入数组。

如果数据一致(即始终使用|和=作为分隔符),您可以使用字符串拆分方法在数组中获取结果。

仅使用拆分不是更容易吗

例如:

var test = "bla bla bla | First Name = John Doe | City = Denver | bla bla bla | State = CA | bla bla bla";
var sections = test.Split('|');
var firstName = sections[1].Split('=')[1].Trim();
var city= sections[2].Split('=')[1].Trim();
var state= sections[4].Split('=')[1].Trim();

使用split不是更容易吗

例如:

var test = "bla bla bla | First Name = John Doe | City = Denver | bla bla bla | State = CA | bla bla bla";
var sections = test.Split('|');
var firstName = sections[1].Split('=')[1].Trim();
var city= sections[2].Split('=')[1].Trim();
var state= sections[4].Split('=')[1].Trim();
使用
Split()
函数:

public class SplitTest {
    public static void Main() {

        string words = "This is a list of words, with: a bit of punctuation" +
                       "\tand a tab character.";

        string [] split = words.Split(new Char [] {' ', ',', '.', ':', '\t' });

        foreach (string s in split) {

            if (s.Trim() != "")
                Console.WriteLine(s);
        }
    }
}
// The example displays the following output to the console:
//       This
//       is
//       a
//       list
//       of
//       words
//       with
//       a
//       bit
//       of
//       punctuation
//       and
//       a
//       tab
//       character
使用
Split()
函数:

public class SplitTest {
    public static void Main() {

        string words = "This is a list of words, with: a bit of punctuation" +
                       "\tand a tab character.";

        string [] split = words.Split(new Char [] {' ', ',', '.', ':', '\t' });

        foreach (string s in split) {

            if (s.Trim() != "")
                Console.WriteLine(s);
        }
    }
}
// The example displays the following output to the console:
//       This
//       is
//       a
//       list
//       of
//       words
//       with
//       a
//       bit
//       of
//       punctuation
//       and
//       a
//       tab
//       character

使用命名组非常简单

    // named groups are very cool for this...
    public static Regex regex = new Regex("\\|(?:\\s*)(?<key>(\\w+)(\\s*))=(?<value>[^|]+)", RegexOptions.CultureInvariant | RegexOptions.Compiled);

    public static Dictionary<string, string> Extract(string line)
    {
        Dictionary<string, string> results = new Dictionary<string, string>();          
        foreach (Match match in regex.Matches(line))
        {
            var groupKey = match.Groups["key"];
            var groupValue = match.Groups["value"];
            if (groupKey.Success && groupValue.Success)
            {
                // add the group value trimmed as we might have extra blank spaces
                results[groupKey.Value.Trim()] = groupValue.Value.Trim();
            }
        }
        return results;
    }
//命名组在这方面非常酷。。。
public static Regex Regex=new Regex(“\\\\\(?:\\s*)(?(\\w+)(\\s*)=(?[^\\]+)”,RegexOptions.CultureInvariant | RegexOptions.Compiled);
公共静态词典提取(字符串行)
{
字典结果=新字典();
foreach(正则表达式中的匹配项。匹配项(行))
{
var groupKey=match.Groups[“key”];
var groupValue=match.Groups[“value”];
if(groupKey.Success&&groupValue.Success)
{
//添加组值修剪,因为我们可能有额外的空格
结果[groupKey.Value.Trim()]=groupValue.Value.Trim();
}
}
返回结果;
}

使用命名组非常简单

    // named groups are very cool for this...
    public static Regex regex = new Regex("\\|(?:\\s*)(?<key>(\\w+)(\\s*))=(?<value>[^|]+)", RegexOptions.CultureInvariant | RegexOptions.Compiled);

    public static Dictionary<string, string> Extract(string line)
    {
        Dictionary<string, string> results = new Dictionary<string, string>();          
        foreach (Match match in regex.Matches(line))
        {
            var groupKey = match.Groups["key"];
            var groupValue = match.Groups["value"];
            if (groupKey.Success && groupValue.Success)
            {
                // add the group value trimmed as we might have extra blank spaces
                results[groupKey.Value.Trim()] = groupValue.Value.Trim();
            }
        }
        return results;
    }
//命名组在这方面非常酷。。。
public static Regex Regex=new Regex(“\\\\\(?:\\s*)(?(\\w+)(\\s*)=(?[^\\]+)”,RegexOptions.CultureInvariant | RegexOptions.Compiled);
公共静态词典提取(字符串行)
{
字典结果=新字典();
foreach(正则表达式中的匹配项。匹配项(行))
{
var groupKey=match.Groups[“key”];
var groupValue=match.Groups[“value”];
if(groupKey.Success&&groupValue.Success)
{
//添加组值修剪,因为我们可能有额外的空格
结果[groupKey.Value.Trim()]=groupValue.Value.Trim();
}
}
返回结果;
}

但是我如何能够捕获键值对,如城市、州、名字等。但是我如何能够捕获键值对,如城市、州、名字等。