Can anyone recommend a method to perform the following string operation using C# -


suppose have string:

"my event happened in new york on broadway in 1976"

i have many such strings, locations , dates vary. example:

"my event happened in boston on 2nd street in 1998" "my event happened in ann arbor on washtenaw in 1968"

so general form is: "my event happened in x on y in z"

i parse string extract x, y , z

i use split , use sentinel words "in", "on" delimit token want seems clunky. using full parser/lexer grammatica seems heavyweight.

recommendations gratefully accepted.

is there "simple" parser lexer c#?

try using regex pattern matching. here's msdn link should pretty helpful: http://support.microsoft.com/kb/308252


an example might help. note regex solution gives scope accept more variants , when see them. reject idea regex overkill, way. i'm no expert it's easy stuff wonder why it's not used more frequently.

var regex = new regex(         "(?<intro>.+) in (?<city>.+) on (?<locality>.+) in (?<eventdate>.+)"         );  var match = regex.match("my event happens in baltimore on main street in 1876.");  if (!match.success) return; foreach (var group in new[] {"intro", "city", "locality", "eventdate"}) {     console.writeline(group + ":" + match.groups[group]); } 

finally, if performance real worry (though ignore if isn't), here optimisation tips.


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -