Reputation: 4198
Hi I have firmly simple question, but i am not an regex ace: i have a string that looks something like this:
Some text
Error codes:
10001 iTPM full self test
10003 less than minimum required
10004 bad tag value
10005 bad param size
10006 fail check
And using regex I am trying to get text from Error codes:, but without it, to the end of string
So far I've got:
(?<=Error codes:\n)(?s)(.*?)(fail check)
It works but its a stretch solution, I want to replace this last group with read till end but so far no luck.
Text contains line breakers as this info is needed.
Lets say c# will be my choice of language
Expected outcome shold look like:
10001 iTPM full self test
10003 less than minimum required
10004 bad tag value
10005 bad param size
10006 fail check
I want to read to the end of string as I cannot be sure if some new codes will not be added.
Upvotes: 1
Views: 107
Reputation: 186668
If "Lets say c# will be my choice of language" I suggest combining Linq and regular expressions:
using System.Linq;
using System.Text.RegularExpressions;
...
string source =
@"Some text
Error codes:
10001 iTPM full self test
10003 less than minimum required
10004 bad tag value
10005 bad param size
10006 fail check";
var result = source
.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)
.SkipWhile(line => !line.StartsWith("Error codes:"))
.Select(line => Regex.Match(line, @"^(?<code>[0-9]+)\s*(?<name>.+)$"))
.Where(match => match.Success) // Or .TakeWhile(match => match.Success)
.Select(match => $"{match.Groups["code"].Value} {match.Groups["name"].Value}")
.ToArray(); // let's represent result as an array
Test:
Console.Write(string.Join(Environment.NewLine, result));
Outcome:
10001 iTPM full self test
10003 less than minimum required
10004 bad tag value
10005 bad param size
10006 fail check
Upvotes: 1
Reputation: 7476
Try with below regex, lookbehind from Error codes with two line breaks.
(?<=Error codes:\n\n)[\w\s]+
Upvotes: 1