[已解决]通过可配置格式进行解析 [英] [Solved] Parsing by configurable format
问题描述
你好,
这是我的问题:
我的应用程序从上层系统接收表示坐标R,X,Y和Z的字符串.到目前为止,Z始终为1,因此被忽略了-输入的字符串格式为RR-XXX-YY,但是现在我收到的格式为RR-XXX-YYZ而且我的解决方案必须涵盖所有可能的格式(目前只有这两种格式,但您永远都不知道).
因此,在应用程序设置XML文件中,我将具有表示当前使用格式的字符串,例如RR-XXX-YY-Z(或其他)和我正在寻找的功能应根据定义的格式为每个坐标提取相应的整数.
任何帮助或提示将不胜感激.谢谢,蒂恩
我知道如何解决此逐位",但我希望对此有一些优雅的解决方案,也许使用一些我不知道的第三方字符串库.
Hello,
here is my problem:
my application receives string from upper system representing coordinates R, X, Y and Z. Until now Z was always 1 and therefore ignored - the input string format was RR-XXX-YY, but now I receive string in format RR-XXX-YYZ and my solution must cover all possible formats (for now just these two, but you never know).
So, in application settings XML file I would have string representing the currently used format e.g. RR-XXX-YY-Z (or whatever) and the function I am looking for should extract corresponding integers for each coordinate based on defined format.
Any help or tips will be greatly appreciated. Thanks, Tine
I know how to solve this "bit-by-bit", but I hope there is some elegant solution for this, perhaps using some 3rd party string library I don''t know of.
推荐答案
如果字符串总是在变化,那么您怎么可能希望编写一个正确解析它的方法?
您可以从此开始并根据需要对其进行修改:
If the string is always changing, how can you possibly hope to write a method that correctly parses it?
You could start with this and modify it as necessary:
string myString = "11-222-33-4";
int r = -1;
int x = -1;
int y = -1;
int z = -1;
string[] parts = myString.Split("-");
for (int i = 0; i < parts.Length; i++)
{
switch (i)
{
case 0 : int.TryParse(parts[i], out r); break;
case 1 : int.TryParse(parts[i], out x); break;
case 2 : int.TryParse(parts[i], out y); break;
case 3 : int.TryParse(parts[i], out z); break;
}
}
此时,您可以在转换值时做任何需要的事情.
At that point, you can do whatever you need when you''re converting the value.
下面是我想出的解决方案.它当然会在发布之前进行重构,但是可以解决问题.此外,大多数慢速"代码仅在应用程序启动时运行.
如果有人有更有效的方法,请告诉我.
Below is the solution I came up with. It will off course be re-factored before release, but it does the trick. Besides most of the "slow" code will be run only when application starts.
If somebody has a more efficient approach, please let me know.
private void OnParse(object sender, EventArgs e)
{
string format = this.textBoxFormat.Text;
string input = this.textBoxInput.Text;
int rack = 0;
int x = 0;
int y = 0;
int z = 0;
// very simple regular expressions for each coordinate
System.Text.RegularExpressions.Regex rackPattern =
new System.Text.RegularExpressions.Regex(
"R+", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex xPattern =
new System.Text.RegularExpressions.Regex(
"X+", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex yPattern =
new System.Text.RegularExpressions.Regex(
"Y+", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex zPattern =
new System.Text.RegularExpressions.Regex(
"Z+", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
// execute pattern matching
System.Text.RegularExpressions.MatchCollection rackMatches =
rackPattern.Matches(format);
System.Text.RegularExpressions.MatchCollection xMatches =
xPattern.Matches(format);
System.Text.RegularExpressions.MatchCollection yMatches =
yPattern.Matches(format);
System.Text.RegularExpressions.MatchCollection zMatches =
zPattern.Matches(format);
// validate format: if more than one match is found, then format is invalid
if ((rackMatches.Count > 1) ||
(xMatches.Count > 1) ||
(yMatches.Count > 1) ||
(zMatches.Count > 1))
{
MessageBox.Show(
"Invalid format!",
"Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
return;
}
// do parsing only if exactly one match is found
// if none found, then coordinate value defaults to 0
if (rackMatches.Count == 1)
{
string rackMatch = rackMatches[0].ToString();
int rackMatchLength = rackMatch.Length;
int rackMatchPosition = format.IndexOf(rackMatch);
try
{
rack = Int32.Parse(input.Substring(rackMatchPosition, rackMatchLength));
}
catch (Exception ex)
{
MessageBox.Show(
ex.Message,
"Invalid input",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
if (xMatches.Count == 1)
{
string xMatch = xMatches[0].ToString();
int xMatchLength = xMatch.Length;
int xMatchPosition = format.IndexOf(xMatch);
try
{
x = Int32.Parse(input.Substring(xMatchPosition, xMatchLength));
}
catch (Exception ex)
{
MessageBox.Show(
ex.Message,
"Invalid input",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
if (yMatches.Count == 1)
{
string yMatch = yMatches[0].ToString();
int yMatchLength = yMatch.Length;
int yMatchPosition = format.IndexOf(yMatch);
try
{
y = Int32.Parse(input.Substring(yMatchPosition, yMatchLength));
}
catch (Exception ex)
{
MessageBox.Show(
ex.Message,
"Invalid input",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
if (zMatches.Count == 1)
{
string zMatch = zMatches[0].ToString();
int zMatchLength = zMatch.Length;
int zMatchPosition = format.IndexOf(zMatch);
try
{
z = Int32.Parse(input.Substring(zMatchPosition, zMatchLength));
}
catch (Exception ex)
{
MessageBox.Show(
ex.Message,
"Invalid input",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
// populate textboxes
this.textBoxRack.Text = rack.ToString();
this.textBoxX.Text = x.ToString();
this.textBoxY.Text = y.ToString();
this.textBoxZ.Text = z.ToString();
}
这篇关于[已解决]通过可配置格式进行解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!