How to extract data from a text file
- by šljaker
I need to parse some large text file and extract Display name and area code only if it matches the following pattern:
Line starts with display name (any number of words)
Followed by 6 digits (can contain spaces)
Followed by #text tag
e.g.
John doe 123 456 #text some text
Display name: John doe
Area code: 123 456
Test 123456 #text
Display name: Test
Area code: 123456
Test 123 #test
Invalid, area code contains only 3 digits
Test 123456 #test1
Invalid, contains invalid tag
Test 123g45 #test
Invalid, area code contains letters
etc.
I know how to open the text file and read it line by line, but having trouble with writing the regular expression.
Any help would be greatly appreciated!
edit:
I have tried this:
private static void Main(string[] args)
{
string text = "John Doe 123 45 #text Lorem ipsum dolor :)";
string pattern = @"(\w+)*([0-9]{2,5}).([0-9]{2,5}).#text";
Match match = Regex.Match(text, pattern, RegexOptions.IgnoreCase);
if (match.Success)
{
string key = match.Groups[0].Value;
Console.WriteLine(key);
}
}