Regular Expressions [modified]
-
I am learning how to read text files at the moment, and it seems regular expressions is the way to go for what I need. I understand the concept but not quite sure how to implement it. I have the line... $2 USD NL Texas Hold'em - Sunday, August 09, 19:05:42 EDT 2009 ...stored in a string. I would like to take certain parts of this and store them in seperate strings. The data I need is... NL Texas Hold'em August 09 19:05:42 2009 Should I use something like "USD\s.*\s-" this would give me "USD NL Texas Hold'Em -"? After this I can then trim "USD " and " -" off the start and the end of the string and I will the information I need.
Regex rExp = new Regex("USD\\s.*\\s-");
Match match = rExp.Match(sLine1);string gameType = match.Value;
gameType.TrimStart("USD ".ToCharArray());
gameType.TrimEnd(" -".ToCharArray());switch (gameType)
{
case "NL Texas Hold'em":
// Do something
break;
default:
break;
}Is this a good way to approach the task? Are there better ways to do this? Any help would be appreciated. Thank you.
modified on Monday, August 10, 2009 1:18 AM
-
I am learning how to read text files at the moment, and it seems regular expressions is the way to go for what I need. I understand the concept but not quite sure how to implement it. I have the line... $2 USD NL Texas Hold'em - Sunday, August 09, 19:05:42 EDT 2009 ...stored in a string. I would like to take certain parts of this and store them in seperate strings. The data I need is... NL Texas Hold'em August 09 19:05:42 2009 Should I use something like "USD\s.*\s-" this would give me "USD NL Texas Hold'Em -"? After this I can then trim "USD " and " -" off the start and the end of the string and I will the information I need.
Regex rExp = new Regex("USD\\s.*\\s-");
Match match = rExp.Match(sLine1);string gameType = match.Value;
gameType.TrimStart("USD ".ToCharArray());
gameType.TrimEnd(" -".ToCharArray());switch (gameType)
{
case "NL Texas Hold'em":
// Do something
break;
default:
break;
}Is this a good way to approach the task? Are there better ways to do this? Any help would be appreciated. Thank you.
modified on Monday, August 10, 2009 1:18 AM
Ah, thanks, I was just working on a Regex tester; you gave me something to test! :-D With that input, this Regex:
\$\d* \w* (?'Game'.*?) -.*?, (?'Date'.*?), (?'Time'\d\d:\d\d:\d\d) \w* (?'Year'\d*)
Produces:Match: $2 USD NL Texas Hold'em - Sunday, August 09, 19:05:42 EDT 2009
Group 0: $2 USD NL Texas Hold'em - Sunday, August 09, 19:05:42 EDT 2009
Group Game: NL Texas Hold'em
Group Date: August 09
Group Time: 19:05:42
Group Year: 2009(You should also look into Expresso.)
-
Ah, thanks, I was just working on a Regex tester; you gave me something to test! :-D With that input, this Regex:
\$\d* \w* (?'Game'.*?) -.*?, (?'Date'.*?), (?'Time'\d\d:\d\d:\d\d) \w* (?'Year'\d*)
Produces:Match: $2 USD NL Texas Hold'em - Sunday, August 09, 19:05:42 EDT 2009
Group 0: $2 USD NL Texas Hold'em - Sunday, August 09, 19:05:42 EDT 2009
Group Game: NL Texas Hold'em
Group Date: August 09
Group Time: 19:05:42
Group Year: 2009(You should also look into Expresso.)
Thanks Piebald, thats the sort of expression I was looking for. Far more simpl than what I had originally.
// Parse line 2 to find game type, date, and time of hand
Regex regExp = new Regex("$\\d* USD (?'GameType'.*?) -.*?, (?'DateMonth'\\w*?) (?'DateDay'\\d*?), (?'DateTime'\\d{2}:\\d{2}:\\d{2}?) \\w* (?'DateYear' \\d*?)");
Match line2Match = regExp.Match(sLine2);switch (line2Match.Groups["GameType"].Value)
{
case "NL Texas Hold'em":
// Do something
break;
default:
break;
} -
Thanks Piebald, thats the sort of expression I was looking for. Far more simpl than what I had originally.
// Parse line 2 to find game type, date, and time of hand
Regex regExp = new Regex("$\\d* USD (?'GameType'.*?) -.*?, (?'DateMonth'\\w*?) (?'DateDay'\\d*?), (?'DateTime'\\d{2}:\\d{2}:\\d{2}?) \\w* (?'DateYear' \\d*?)");
Match line2Match = regExp.Match(sLine2);switch (line2Match.Groups["GameType"].Value)
{
case "NL Texas Hold'em":
// Do something
break;
default:
break;
}Glad to be of service.