Parse机场滑行道标签

时间:2014-03-27 02:36:03

标签: c# regex

我正在编写一个c#方法,将机场滑行道从其原始格式转换为书面版本......例如:

AA将被写为ALPHA ALPHA

F1FOXTROT 1

查看洛杉矶机场图以获取更多示例:https://www.dropbox.com/s/8614gp41dwnwvsm/klax.PDF

这是我到目前为止所做的:

public static string ParseTaxiway(string taxiway)
{
    string twy = "";
    var alpha = Regex.Match(taxiway, "([A-Z]){1,2}");
    var alphanum = Regex.Match(taxiway, "([A-Z])([1-9])");

    // do we have an alpha character only taxiway?
    if(alpha.Success)
    {
        switch(alpha.Groups[1].Value)
        {
            case "A":
                twy = "ALPHA";
                break;
            case "B":
                twy = "BRAVO";
                break;
            case "C":
                twy = "CHARLIE";
                break;
            case "D":
                twy = "DELTA";
                break;
            case "E":
                twy = "ECHO";
                break;
            case "F":
                twy = "FOXTROT";
                break;
            case "G":
                twy = "GOLF";
                break;
            case "H":
                twy = "HOTEL";
                break;
            case "I":
                twy = "INDIA";
                break;
            case "J":
                twy = "JULIET";
                break;
            case "K":
                twy = "KILO";
                break;
            case "L":
                twy = "LIMA";
                break;
            case "M":
                twy = "MIKE";
                break;
            case "N":
                twy = "NOVEMBER";
                break;
            case "O":
                twy = "OSCAR";
                break;
            case "P":
                twy = "PAPA";
                break;
            case "Q":
                twy = "QUEBEC";
                break;
            case "R":
                twy = "ROMEO";
                break;
            case "S":
                twy = "SIERRA";
                break;
            case "T":
                twy = "TANGO";
                break;
            case "U":
                twy = "UNIFORM";
                break;
            case "V":
                twy = "VICTOR";
                break;
            case "W":
                twy = "WHISKEY";
                break;
            case "X":
                twy = "XRAY";
                break;
            case "Y":
                twy = "YANKEE";
                break;
            case "Z":
                twy = "ZULU";
                break;
        }
    }

    // taxiway with letter and number; A1, F5, B6, etc
    if(alphanum.Success)
    {
        switch (alphanum.Groups[1].Value)
        {
            case "A":
                twy = "ALPHA " + alphanum.Groups[2].Value);
                break;
            case "B":
                twy = "BRAVO " + alphanum.Groups[2].Value);
                break;
            case "C":
                twy = "CHARLIE " + alphanum.Groups[2].Value);
                break;
            case "D":
                twy = "DELTA " + alphanum.Groups[2].Value);
                break;
            case "E":
                twy = "ECHO " + alphanum.Groups[2].Value);
                break;
            case "F":
                twy = "FOXTROT " + alphanum.Groups[2].Value);
                break;
            case "G":
                twy = "GOLF " + alphanum.Groups[2].Value);
                break;
            case "H":
                twy = "HOTEL " + alphanum.Groups[2].Value);
                break;
            case "I":
                twy = "INDIA " + alphanum.Groups[2].Value);
                break;
            case "J":
                twy = "JULIET " + alphanum.Groups[2].Value);
                break;
            case "K":
                twy = "KILO " + alphanum.Groups[2].Value);
                break;
            case "L":
                twy = "LIMA " + alphanum.Groups[2].Value);
                break;
            case "M":
                twy = "MIKE " + alphanum.Groups[2].Value);
                break;
            case "N":
                twy = "NOVEMBER " + alphanum.Groups[2].Value);
                break;
            case "O":
                twy = "OSCAR " + alphanum.Groups[2].Value);
                break;
            case "P":
                twy = "PAPA " + alphanum.Groups[2].Value);
                break;
            case "Q":
                twy = "QUEBEC " + alphanum.Groups[2].Value);
                break;
            case "R":
                twy = "ROMEO " + alphanum.Groups[2].Value);
                break;
            case "S":
                twy = "SIERRA " + alphanum.Groups[2].Value);
                break;
            case "T":
                twy = "TANGO " + alphanum.Groups[2].Value);
                break;
            case "U":
                twy = "UNIFORM " + alphanum.Groups[2].Value);
                break;
            case "V":
                twy = "VICTOR " + alphanum.Groups[2].Value);
                break;
            case "W":
                twy = "WHISKEY " + alphanum.Groups[2].Value);
                break;
            case "X":
                twy = "XRAY " + alphanum.Groups[2].Value);
                break;
            case "Y":
                twy = "YANKEE " + alphanum.Groups[2].Value);
                break;
            case "Z":
                twy = "ZULU " + alphanum.Groups[2].Value);
                break;
        }
    }

    return twy;
}

我遇到的问题是如果我有一个标有AA的滑行道,它只会返回ALPHA。滑行道限制为一个或两个字母长一个字母,一个数字(1-9)。 R,AA,AC,D1,ZZ等...所以有很多组合。

如果有人可以指导我正确创建这种解析方法,我非常感激。

5 个答案:

答案 0 :(得分:2)

这种方法试图减少样板代码的数量。

public static class Taxiway
{
    private static Dictionary<char, string> lookup =
        new string[]
        {
            "ALPHA",
            "BRAVO",
            "CHARLIE",
            ...
            "ZULU"
        }
        .Concat(Enumerable.Range(1, 9).Select(n => n.ToString()))
        .ToDictionary(s => s[0]);

    public static string Parse(string s)
    {
        if (s == null || s.Length < 1 || s.Length > 2
            || !s.All(c => lookup.ContainsKey(c))
            || !char.IsLetter(s[0]))
        {
            throw new ArgumentException("Invalid taxiway.", "s");
        }
        return string.Join(" ", s.Select(c => lookup[c]));
    }
}

Taxiway.Parse("W5")会给你"WHISKEY 5"
Taxiway.Parse("C")会给你"CHARLIE"
Taxiway.Parse("CA")将为您提供"CHARLIE ALPHA"

它只接受以下形式之一的字符串:
"{A-Z}""{A-Z}{A-Z}""{A-Z}{1-9}"

除此之外:ParseX建议将string转换为X的方法。您可能想要考虑不同的名称。

答案 1 :(得分:1)

只需将键/值映射存储到字典中,然后逐个字符地循环输入字符串以获取单词并创建最终输出。

var dictionary = new Dictionary<char, string>();

dictionary.Add('A', "Alpha");
// and so on..

public static string ParseTaxiway(string taxiway)
{
 string finalValue = "";

 foreach (var character in taxiway)
 {
  finalValue += dictionary[character] +" ";
 }

 return finalValue;
}

答案 2 :(得分:0)

这些人谈论做一些PHP称为strtr()的事情。基本上你喂它一个字符串,它返回一个字符的翻译。这类东西真的很方便:

http://forums.devshed.com/programming-42/equalvilent-method-strtr-functon-php-541398.html

否则,您可以尝试在单个维度中使用所有字母/数字词典的有序字典,如下所示:

OrderedDictionary alphas = new OrderedDictionary();

// add your alphas
alphas.Add("A", "ALPHA");
alphas.Add("B", "BRAVO");
..etc

然后通过索引引用它们来构建你的字符串(alpha是你的正则表达式单字符值)

twy = alphas[alpha] + alphanum.Groups[2].Value;

对于您尝试做的事情,构建一个字符串,此方法将保存一些代码并且不需要业务逻辑。

答案 3 :(得分:0)

您可以将代码和翻译存储在字典中,然后使用LINQ翻译每个字符:

var dict = new Dictionary<char, string>
{
    { 'A', "ALPHA" },
    { 'B', "BRAVO" },
    ...
    { 'F', "FOXTROT" },
    ...
    { '1', "1" },
    { '2', "2" },
    ...
};

var code = "AA";

var translation = string.Join(" ", code.Select(c => dict[c]));

输出:

ALPHA ALPHA   // AA

FOXTROT 1     // F1

答案 4 :(得分:0)

这是一个存根

enum MyEnum
{
    ALPHA,
    BRAVO
}
String taxiNo= "AB";
String convertedString = "";
if(taxiNo.Length == 0)
{
    // throw exception
}
else
    convertedString = ((MyEnum)((int)(taxiNo.ToCharArray()[0])-65)).ToString();
if(taxiNo.Length == 2)
{
    int justForCheck;
    if(!int.TryParse(taxiNo.Substring(1,1),out justForCheck))
        convertedString += " " + ((MyEnum)((int)(taxiNo.ToCharArray()[1])-65)).ToString();
    else
        convertedString += " " + taxiNo[1].ToString();
}