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

Suppose I have a line:

"my event took place in New York on Broadway in 1976"

I have many such lines, but places and dates are changing. For instance:

"my event took place in Boston on 2nd Street in 1998." "my event took place in Ann Arbor in Ushtenau in 1968."

so the general view: "my event occurred in X on Y in Z"

I would like to parse a string to extract X, Y and Z

I could use Split and use the sentinels "in", "on" to demarcate the token I want, but that seems awkward. But using a full parser / lexer, such as grammatica, seems heavyweight.

Recommendations will be gratefully accepted.

Is there a "simple" parser lexer for C #?

+3
source share
3 answers

Try matching regular expression patterns. Here's the MSDN link, which should be very helpful: http://support.microsoft.com/kb/308252


An example may help. Please note that the regex solution gives you the opportunity to take more options as they become available. I reject the idea that RegEx, by the way, went too far. I am not an expert, but it is so easy to do such things. I wonder why it is not used more often.

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 is a real concern (though ignore it if it is not), look here for optimization tips.

0

KISS. String.Split String.IndexOf, "in" "out" ( , String.Split ). ""; , , regex .

+4

, , , , "" , "on".

, .


, .
0

Source: https://habr.com/ru/post/1790304/


All Articles