VS2008 C# : Regular expression and identifying certain words
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 Fir开发者_如何学JAVAst 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.
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();
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
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;
}
I would use string.Split('|') and string.IndexOf("=") to parse the elements. It would certainly be more straightforward than regex.
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.
精彩评论