L
Lincoln_MA
Guest
Hello fs - ab.
I see that your question (with the same title) is locked so I cannot reply directly, but I did want to take a moment to provide a guideline which you may find useful in future endeavors ….
string entries =
"Deposits/Transfer Ins 0.00 0.00 0.00" +
"Withdrawals/Transfer Outs 0.00 0.00 -15,660.70" +
"Total Investment Return -272.14 -572.63 8,454.91";
const string leader = @"(?<leader>[A-Za-z\s/]+)";
const string ConventionalReal = @"\s*\d+\.\d+";
const string PrettyPrintReal = @"\s*(\d{1,3},?)+\d{3}\.\d+";
const string RealNo =
@"\s*[+-]?(" + ConventionalReal + "|" + PrettyPrintReal + ")";
const string TestPattern =
leader +
@"(?<first>" + RealNo + ")" +
@"(?<second>" + RealNo + ")" +
@"(?<third>" + RealNo + ")";
string leaderString, firstString, secondString, thirdString;
Regex AccountInfo = new Regex(TestPattern);
MatchCollection info;
info = AccountInfo.Matches(entries);
// verify that we have the result we seek.
foreach (Match match in info) {
leaderString = match.Groups["leader"].Value;
firstString = match.Groups["first"].Value;
secondString = match.Groups["second"].Value;
thirdString = match.Groups["third"].Value;
}
The code above is NOT the solution you seek, but it may serve as a guideline for your efforts
to find that solution. Note however that I took the liberty of changing a few items to do things
in a manner that pleases ME.
For instance, I converted your string array to a single string. That's just how I roll. Whenever
possible, I use a single string and a MatchCollection. Again, it's just personal preference. If your
circumstances require 'entries' to be a string array, then fine, let it be. Really, in this example,
that's just a detail ... it's 'TestPattern' that's the important thing. Note how I broke it into
elements ('leader', 'ConventionalReal', 'PrettyPrintReal', 'RealNo'), then gathered them into
'TestPattern'. I do that because in my experience, that makes debugging SO MUCH EASIER.
Again, it's a personal thing. Your style may differ significantly. That's cool.
The 'foreach' statement is there simply to confirm that I have the desired results. It's a
debugging aid.
Note also that I use named captures. Again, it's just something that pleases me. You may wish
to use numbered captures.
A final comment: note that my TestPattern significantly differs from your original post.
The reason is that Regular Expressions are a pattern-matching mechanism. It seems to me that
the form of the pattern string expresses a certain 'Rigidity' which ranges from wide-open
(it'll match darn near ANYthing) to iron-clad, inflexibility. For ME, Rigidity should blend
both extremes to produce a pattern that provides some assurance that, in this case for instance,
the input really 'looks' like an acccount summary.
In summary then, I changed several details to suit myself. You need not follow my style.
The important thing is a glimpse into a Regular Expression that works (at least on my 'chine).
I have NOT tested this in-depth. Certainly it has many bugs that rigorous testing will reveal,
but it's a start ... something to consider as you proceed with your search for a solution that
works for your requirements.
Continue reading...
I see that your question (with the same title) is locked so I cannot reply directly, but I did want to take a moment to provide a guideline which you may find useful in future endeavors ….
string entries =
"Deposits/Transfer Ins 0.00 0.00 0.00" +
"Withdrawals/Transfer Outs 0.00 0.00 -15,660.70" +
"Total Investment Return -272.14 -572.63 8,454.91";
const string leader = @"(?<leader>[A-Za-z\s/]+)";
const string ConventionalReal = @"\s*\d+\.\d+";
const string PrettyPrintReal = @"\s*(\d{1,3},?)+\d{3}\.\d+";
const string RealNo =
@"\s*[+-]?(" + ConventionalReal + "|" + PrettyPrintReal + ")";
const string TestPattern =
leader +
@"(?<first>" + RealNo + ")" +
@"(?<second>" + RealNo + ")" +
@"(?<third>" + RealNo + ")";
string leaderString, firstString, secondString, thirdString;
Regex AccountInfo = new Regex(TestPattern);
MatchCollection info;
info = AccountInfo.Matches(entries);
// verify that we have the result we seek.
foreach (Match match in info) {
leaderString = match.Groups["leader"].Value;
firstString = match.Groups["first"].Value;
secondString = match.Groups["second"].Value;
thirdString = match.Groups["third"].Value;
}
The code above is NOT the solution you seek, but it may serve as a guideline for your efforts
to find that solution. Note however that I took the liberty of changing a few items to do things
in a manner that pleases ME.
For instance, I converted your string array to a single string. That's just how I roll. Whenever
possible, I use a single string and a MatchCollection. Again, it's just personal preference. If your
circumstances require 'entries' to be a string array, then fine, let it be. Really, in this example,
that's just a detail ... it's 'TestPattern' that's the important thing. Note how I broke it into
elements ('leader', 'ConventionalReal', 'PrettyPrintReal', 'RealNo'), then gathered them into
'TestPattern'. I do that because in my experience, that makes debugging SO MUCH EASIER.
Again, it's a personal thing. Your style may differ significantly. That's cool.
The 'foreach' statement is there simply to confirm that I have the desired results. It's a
debugging aid.
Note also that I use named captures. Again, it's just something that pleases me. You may wish
to use numbered captures.
A final comment: note that my TestPattern significantly differs from your original post.
The reason is that Regular Expressions are a pattern-matching mechanism. It seems to me that
the form of the pattern string expresses a certain 'Rigidity' which ranges from wide-open
(it'll match darn near ANYthing) to iron-clad, inflexibility. For ME, Rigidity should blend
both extremes to produce a pattern that provides some assurance that, in this case for instance,
the input really 'looks' like an acccount summary.
In summary then, I changed several details to suit myself. You need not follow my style.
The important thing is a glimpse into a Regular Expression that works (at least on my 'chine).
I have NOT tested this in-depth. Certainly it has many bugs that rigorous testing will reveal,
but it's a start ... something to consider as you proceed with your search for a solution that
works for your requirements.
Continue reading...