R
raylopez99
I went through a bunch of Regex examples, and indeed it's quite
powerful, including 'groups' using 'matches', word boundaries,
lookahead matches, replacing and splitting text,etc--apparently
there's a whole book on it, though I just used the chapter in
Albahari et al C#3 in a Nutshell (a great book). You can indeed
'massage' a string into a number of ways, but, at the end of the
process I found that you still have to go through the 'massaged'
string, character by character, and do "fine tuning". I'm sure you
can also do a Regex to do such "fine tuning" but I am at a dead end
for a number of problems and nothing beats a character by character
analysis.
For example, suppose you want to extract all the numbered coefficents
10a, 11d, 12g and 100f from the following: given the string
"xyz10abc11def12gh100f".
string sMY = "xyz10abc11def12gh100f";
Match mMY = Regex.Match(sMY, @"\d\d\D");
Console.WriteLine("result mMY-->{0}", mMY.Value);
Regex r001 = new Regex(@"\d\d\D");
MatchCollection mc2 = r001.Matches(sMY);
foreach (Match m in mc2)
{
Console.WriteLine("m: {0}", m.Value);
}
You'll get output (as an array of strings): 10a, 11d, 12g, 00f, which
is not quite what you want, because of the last value is "00f" not
"100f". So you still have to go through the string "manually" and
extract these "end cases" or "corner cases".
Now you Regex experts I'm sure can find the exact Regex to give the
proper answer (it would help if there's a 'conditional' Regex
expression so you can do either two digits or three digits, but I'm
not aware of one), however, in the general case, I think I'm correct
in saying that while Regex helps, it cannot get all the corner cases.
RL
powerful, including 'groups' using 'matches', word boundaries,
lookahead matches, replacing and splitting text,etc--apparently
there's a whole book on it, though I just used the chapter in
Albahari et al C#3 in a Nutshell (a great book). You can indeed
'massage' a string into a number of ways, but, at the end of the
process I found that you still have to go through the 'massaged'
string, character by character, and do "fine tuning". I'm sure you
can also do a Regex to do such "fine tuning" but I am at a dead end
for a number of problems and nothing beats a character by character
analysis.
For example, suppose you want to extract all the numbered coefficents
10a, 11d, 12g and 100f from the following: given the string
"xyz10abc11def12gh100f".
string sMY = "xyz10abc11def12gh100f";
Match mMY = Regex.Match(sMY, @"\d\d\D");
Console.WriteLine("result mMY-->{0}", mMY.Value);
Regex r001 = new Regex(@"\d\d\D");
MatchCollection mc2 = r001.Matches(sMY);
foreach (Match m in mc2)
{
Console.WriteLine("m: {0}", m.Value);
}
You'll get output (as an array of strings): 10a, 11d, 12g, 00f, which
is not quite what you want, because of the last value is "00f" not
"100f". So you still have to go through the string "manually" and
extract these "end cases" or "corner cases".
Now you Regex experts I'm sure can find the exact Regex to give the
proper answer (it would help if there's a 'conditional' Regex
expression so you can do either two digits or three digits, but I'm
not aware of one), however, in the general case, I think I'm correct
in saying that while Regex helps, it cannot get all the corner cases.
RL