Reputation: 230
I have the following tables from a dotnet test
command result, and what I am trying to achieve is getting the second occurrence (the ones below Branch) of lines that start with Average.
+---------+-----------+-----------+-----------+
| | Line | Branch | Method |
+---------+-----------+-----------+-----------+
| Total | 100% | 100% | 100% |
+---------+-----------+-----------+-----------+
| Average | 100% | 100% | 100% |
+---------+-----------+-----------+-----------+
+---------+-----------+-----------+-----------+
| | Line | Branch | Method |
+---------+-----------+-----------+-----------+
| Total | 100% | 100% | 100% |
+---------+-----------+-----------+-----------+
| Average | 100% | 100% | 100% |
+---------+-----------+-----------+-----------+
I have managed writing the following regex ^\| Average *\| (\d+.\d+\%).*$
but adding {2}
anywhere inside the expression still doesn't return me the second occurrence. Also, I've tried using https://regex101.com/ but the match information that it shows is the following:
From my understanding I need to get the second group but I think I need a hint or a little bit of help to reach my goal.
Any help? Thanks in advance!
Upvotes: 0
Views: 223
Reputation: 230
Eventually, I got the answer by trial and error.
\| Average \| .*\d+\% +\| *(\d*.\d\%) +\| +\d
will math the column below Branches. Thanks everyone for help!
Upvotes: 0
Reputation: 828
What about this:
string table =
"+---------+-----------+-----------+-----------+" + Environment.NewLine +
"| | Line | Branch | Method |" + Environment.NewLine +
"+---------+-----------+-----------+-----------+" + Environment.NewLine +
"| Total | 100% | 100% | 100% |" + Environment.NewLine +
"+---------+-----------+-----------+-----------+" + Environment.NewLine +
"| Average | 100% | 89% | 100% |" + Environment.NewLine +
"+---------+-----------+-----------+-----------+" + Environment.NewLine +
"" + Environment.NewLine +
"+---------+-----------+-----------+-----------+" + Environment.NewLine +
"| | Line | Branch | Method |" + Environment.NewLine +
"+---------+-----------+-----------+-----------+" + Environment.NewLine +
"| Total | 100% | 100% | 100% |" + Environment.NewLine +
"+---------+-----------+-----------+-----------+" + Environment.NewLine +
"| Average | 100% | 99% | 100% |" + Environment.NewLine +
"+---------+-----------+-----------+-----------+";
MatchCollection matches = Regex.Matches(table, @"(?<=\| Average *\| \d+\% +\| *)\d+\%(?=.*)");
foreach (Match m in matches)
{
Console.WriteLine(m.Value);
}
Outputs:
89%
99%
Update:
I had to find out that .NET (where I built my RegEx) supports quantifiers in lookaround expressions, while other RegEx implementations lack this support.
Therefore the RegEx expression of my solution won't run there.
To solve this, I removed the quantifiers and replaced them with fixed character declarations. This works for a fixed table, but won't work if the layout of the table is dynamic in its width:
(?<=\| Average \| ..\d\% \| )\d+\%(?=.*)
Upvotes: 1
Reputation: 10962
One solution I see is to have a regex that will capture several lines, starting with the first "Average" and ending with the second. As for having all the logic contained in the regex, you then need to know how to specify search the options within your regex, this is usually done using /sm
. In the end your regex would look like this:
/^\| Average *\| \d*.\d+\%.*$.*^\| Average *\| (\d*.\d+\%).*$/sm
The captured group only contains the second occurence of Average
's Line
percentage.
Upvotes: 0