using System.Text.RegularExpressions;
namespace.
Our input string looks like this:
Jacek Skowron ****
Mielec 1984
Address line
description description description description description description description description description description description description description description description description description description description description
Krystian Kapel ***
Szczucin 1983
Addres line
description description description description description description description description description description description description description description description description description description description description
This string contains set of people. Each person is described like that:
First line: Name and rating. Second line: birth place and year, third line contains address and the last line description.
My regex expresssion used to parse details from input string is:
1: (?<name>.+?)\s(?<rating>\*+).*?\n
2: (?<birthPlace>.+?)(?<year>\d{4}?).*?\n
3: (?<address>.+?)\n
4: (?<description>.+?)\n
First line: It could look like this:
1: .+?\s\*+.*?\n
1: (?<name>.+?)
(?<rating>\*+)
Second line:
2: (?<birthPlace>.+?)(?<year>\d{4}?).*?\n
Third and fourth line is just an subset of first line where we were looking for person's name. Regular expression is done. Let's use it in code: Listing - using our expression.
Regex rgx = new Regex(@"(?<name>.+?)\s(?<rating>\*+).*?\n(?<birthPlace>.+?)(?<birthYear>\d{4}?).*?\n(?<address>.+?)\n(?<description>.+?)\n", RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace); MatchCollection m = rgx.Matches(inputstring); foreach (Match item in m) { Person p = new Person (); p.Name= item.Groups["name"].Value; p.Rating= item.Groups["rating"].Value.Length; p.BirthPlace = item.Groups["birthPlace"].Value.Trim(); p.BirthYear= item.Groups["birthYear"].Value.Trim(); p.Address = item.Groups["address"].Value.Trim(); p.Description = item.Groups["description"].Value.Trim(); // Process our person object }Variable inputstring contains text with our string to parse. MatchCollection contains all people parsed by regex. Because we've named groups earlier looping over regex result and finding person's properties is easy and straighforward.
Brak komentarzy:
Prześlij komentarz