tags:

views:

129

answers:

5

I would like to use Regular expression to identify certain words in a string.

For example:

"bla bla bla |   First Name = John Doe | City = Denver | bla bla bla | State = CA | bla bla bla"

In the above string, which is | delimited for words, I want to parse out the content of First Name, City, and State and store them some where like in a hash table.

How do I go about doing that? I think the best way would be is to use Regular expression.

A: 

I would use string.Split('|') and string.IndexOf("=") to parse the elements. It would certainly be more straightforward than regex.

David Chappelle
A: 

If your data is consistent (i.e. always uses the | and = as delimiters), you can use the string split method get you results in array.

Jeffrey Hines
+4  A: 

Wouldn't be easier to just use split?

Example:

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();
AlbertEin
+1  A: 

Use the Split() function:

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
Dave Swersky
+1  A: 

Using named groups is very simple...

 // 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;
 }
Florian Doyon