解析所有可能的不同类型建筑的尺寸输入 [英] Parsing all possible types of varying architectural dimension input

查看:111
本文介绍了解析所有可能的不同类型建筑的尺寸输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写我们公司的产品出库,将采取任何形式的建筑尺寸,我们的用户已经非常熟悉了,从一个字符串转换为double的函数作为输入。这里是我们想成为有效的输入类型列表

I am writing a library for our company's product that will take any kind of architectural dimension that our users are already familiar with as input for a function that converts from a string to a double. Here is the list of input types that we would like to be valid.

输入 |含义| 输出(用双代表英寸)

Input| Meaning | Output(Inches represented by a double)

结果
12.5 | 12英尺六英寸| 150.0


12.5' | 12 Feet and six inches | 150.0

11 | 11英寸| 11.0

11" | 11 Inches | 11.0

3/16 |英寸16分之3| 0.1875

3/16" | 3 sixteenths of an Inch | 0.1875

结果

可以或可以不将英尺和英寸和英寸和十六分之间可以使用空间

11'11 | 11脚和11英寸| 143.0

11' 11" | 11 Feet and 11 Inches | 143.0

11'11 | 11脚和11英寸| 143.0

11'11" | 11 Feet and 11 Inches | 143.0

结果,
可能会或可能不会被英尺和英寸之间使用短划线或英寸和十六分或两者

12'-11之间| 12脚和11英寸| 155.0

12'-11" | 12 Feet and 11 Inches | 155.0

12'11 3/16 | 12脚和11英寸和16分之3| 155.1875

12' 11 3/16" | 12 Feet and 11 Inches and 3 sixteenths | 155.1875

12'11-1 / 2 | 12脚和11英寸和16分之8| 155.5

12' 11-1/2" | 12 Feet and 11 Inches and 8 sixteenths | 155.5

结果
任意可以在英尺和英寸和英寸十六分之之间使用空格数

12'11 1/2 | 12脚和11英寸和16分之8| 155.5

12' 11 1/2" | 12 Feet and 11 Inches and 8 sixteenths | 155.5

结果
另一种简单的格式也可

121103 | 12脚和11英寸和16分之3| 155.1875

121103 | 12 Feet and 11 Inches and 3 sixteenths | 155.1875

结果
否定也有可能在每一个格式

-121103 | 12脚和11英寸和16分之3| -155.1875

-121103 | 12 Feet and 11 Inches and 3 sixteenths | -155.1875

-11'11 | 11脚和11英寸| -143.0

-11'11" | 11 Feet and 11 Inches | -143.0

目前我们使用的是非常复杂的一组分支逻辑的尝试,并确定输入尝试什么样的格式效仿......它并不适用于所有情况。

We are currently using an extremely complicated set of branching logic to try and determine what format the input is trying to emulate... And it doesn't work in all cases.

有LINQ和正则表达式和巫术的一些可能的组合,我们可以用它来确定如何解析字符串?

Is there some possible combination of LINQ and Regular Expressions and witchcraft that we can use to determine how to parse the string?

另外请注意,我们真的想避免给窗体上的简单组合框,选择从输入格式类型。

Also note that we really want to avoid giving a simple combobox on the form to select the input format type from.

推荐答案

此功能适用于您的输入值的例子。

This function works for your input value examples.

public static Double Conv(String inp)
{
    String expr= "((?<feet>\\d+)(?<inch>\\d{2})(?<sixt>\\d{2}))|((?<feet>[\\d.]+)')?[\\s-]*((?<inch>\\d+)?[\\s-]*((?<numer>\\d+)/(?<denom>\\d+))?\")?";
    Match m = new Regex(expr).Match(inp);
    Double feet = m.Groups["feet"].Success ? Convert.ToDouble(m.Groups["feet"].Value) : 0;
    Int32  inch = m.Groups["inch"].Success ? Convert.ToInt32(m.Groups["inch"].Value) : 0;
    Int32  sixt = m.Groups["sixt"].Success ? Convert.ToInt32(m.Groups["sixt"].Value) : 0;
    Int32 numer = m.Groups["numer"].Success ? Convert.ToInt32(m.Groups["numer"].Value) : 0;
    Int32 denom = m.Groups["denom"].Success ? Convert.ToInt32(m.Groups["denom"].Value) : 1;
    return feet*12+inch+sixt/16.0+numer/Convert.ToDouble(denom);
}    

请注意,我没有测试其它投入比作出任何努力您提供的有效的。您可能需要例如检查成功至少一些捕获基团,或者做验证作为一个单独的步骤。这段代码时考虑解析制成

Please note that I haven't made any effort in testing other inputs than the valid ones you supplied. You may want to e.g. check for Success in at least some of the capture groups, or maybe do validation as a separate step. This code was made with parsing in mind.

编辑:

下面是一个更强大的版本:

Here is a more robust version:

public static Double Conv(String inp)
{
    String expr= "^\\s*(?<minus>-)?\\s*(((?<feet>\\d+)(?<inch>\\d{2})(?<sixt>\\d{2}))|((?<feet>[\\d.]+)')?[\\s-]*((?<inch>\\d+)?[\\s-]*((?<numer>\\d+)/(?<denom>\\d+))?\")?)\\s*$";
    Match m = new Regex(expr).Match(inp);
    if(!m.Success || inp.Trim()=="")
    {
        // maybe throw exception or set/return some failure indicator
        return 0; // here using return value zero as failure indicator
    }
    Int32 sign  = m.Groups["minus"].Success ? -1 : 1;
    Double feet = m.Groups["feet"].Success ? Convert.ToDouble(m.Groups["feet"].Value) : 0;
    Int32  inch = m.Groups["inch"].Success ? Convert.ToInt32(m.Groups["inch"].Value) : 0;
    Int32  sixt = m.Groups["sixt"].Success ? Convert.ToInt32(m.Groups["sixt"].Value) : 0;
    Int32 numer = m.Groups["numer"].Success ? Convert.ToInt32(m.Groups["numer"].Value) : 0;
    Int32 denom = m.Groups["denom"].Success ? Convert.ToInt32(m.Groups["denom"].Value) : 1;
    return sign*(feet*12+inch+sixt/16.0+numer/Convert.ToDouble(denom));
}



发生故障,空字符串,并与其他多余的字符的字符串所允许的你例子。
五或多个数字都被视为简单的格式。

It fails for empty strings, and strings with extra characters other than allowed by your examples. Five or more digits are treated as the simpler format.

的变化是开始时间和结束锚,并允许开头和结尾的空白,还有。特殊情况检查,只有空白emtpy /字符串中的if语句

The changes are the begin- and end anchors and allowed leading and trailing whitespace, as well as the special case check for emtpy/whitespace-only string in the if statement.

免责声明:这个显然没有经过测试每一个可能的非法输入,我不是C#程序员反正: - )

Disclaimer: this has obviously not been tested for every possible illegal input, and I am not a c# programmer anyway :-)

这篇关于解析所有可能的不同类型建筑的尺寸输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆