正则表达式,用于限制输入到纬度(以十进制度为单位)文本框中



我有一个带有文本框的 WPF 应用程序,我的用户将在其中输入以十进制度为单位的纬度值(精度最多为 7 位)。 当然,有效纬度范围从 -90.0000000 到 90.0000000。 我正在尝试创建一个正则表达式来限制通过文本框的 PreviewTextInput 事件输入,如下所示:

    private void latitudeTextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
    {
        e.Handled = !ValidateDecimalString(e.Text);
    }
    public static bool ValidateDecimalString(string decimalString)
    {
        Regex regex = new Regex("[^0-9]+");
        return !regex.IsMatch(decimalString);
    }

我当前的正则表达式只允许输入数字,但我还需要强制执行其他一些限制,例如:

  • 纬度可能是负数,所以我需要允许减号 ("-"),但前提是它显示为第一个字符
  • 纬度只能包含一个小数点 (".")

有效纬度值的示例:

  • 90
  • 90,0
  • -90.0000000

我可以仅通过修改正则表达式来实现这些额外的限制吗? 如果是这样,如何? 谢谢!

尝试这样的事情

public static bool ValidateDecimalString(string decimalString)
{
    Regex regex = new Regex(@"^(-)?([0-9]+)(.[0-9]+)?$");
    return regex.IsMatch(decimalString);
}

为了更好地验证范围,请使用转换后的值,例如

public static bool ValidateLatitudeString(string decimalString)
{
    if(ValidateDecimalString(decimalString)){
        double lat = 0;
        return double.TryParse(decimalString, out lat) && lat<=90.0 && lat>=-90;
    }
    return false;
}

所以可能更好的是没有正则表达式,比如

public static bool ValidateLatitudeString(string decimalString)
{
    double lat = 0;
    return double.TryParse(decimalString, out lat) && lat<=90.0 && lat>=-90;
}

虽然你的问题是我如何使用正则表达式验证纬度,但似乎更好的方法是使用Decimal.TryParse之类的东西。

 public static bool ValidateLatitudeString(string decimalString)
    {
        decimal validLatitude;
        if (decimal.TryParse(decimalString, out validLatitude))
        {
            if (validLatitude >= -90.0M && validLatitude <= 90.0M)
            {
                return true;
            }
        }
        return false;
    }

这个怎么样?

public static bool ValidateDecimalString(string decimalString)
{
    Regex regex = new Regex("^-?([0-9]|[1-8][0-9]|90)([.][0-9]*)?$");
    return regex.IsMatch(decimalString);
}

这将允许使用可选的前导连字符减号,后跟一个介于 0 到 90 之间的数字(但不包括 999 或 01),后跟可选的十进制部分。不过,它将允许90.1;要禁止此用途,请执行以下操作:

public static bool ValidateDecimalString(string decimalString)
{
    Regex regex = new Regex("^-?(([0-9]|[1-8][0-9])([.][0-9]*)?|90([.]0*))$");
    return regex.IsMatch(decimalString);
}

这将允许90.0但不允许90.1

演示

<

div class="one_answers"^-?[0-8]?d(?:.d*)?|-?90(?:.0+)?$另一种方式>

有很多选择。单程 -

 #  @"^-?(?:(?:[0-9]|[1-8][0-9])(?:.[0-9]{1,7})?|90(?:.0{1,7})?)$"
 ^ 
 -?
 (?:
      (?:
           [0-9] 
        |  [1-8] [0-9] 
      )
      (?: . [0-9]{1,7} )?
   |  
      90  
      (?: . 0{1,7} )?
 )
 $

匹配的巨型边缘案例

 #  @"^-?(?:(?:[0-9]|[1-8](?:[0-9]|$))(?:.(?:[0-9]{1,7}|$))?|9(?:0|$)(?:.(?:0{1,7}|$))?)?$"
 ^ 
 -?
 (?:
      (?:
           [0-9]  
        |  [1-8] (?: [0-9] | $ )
      )
      (?:
           . (?: [0-9]{1,7} | $ )
      )?
   |  
      9 (?: 0 | $ )
      (?:
           . (?: 0{1,7} | $ )
      )?
 )?
 $

另一种方式(双精度示例) - 不是正则表达式解决方案,但有效

        bool IsDigitsOnlyLatitudaLongituda(string str, int deg)
        {
    // deg with value 0 is latitude, deg with value 1 is longitude
            bool provjera = true;
            int brojactocki = 0;
            if (str.Length > 0)
            {
                if (str[0] == '.')
                {
                    provjera = false;
                }
                if (str[str.Length - 1] == '.')
                {
                    provjera = false;
                }
            }
            var brojac = 0;
            foreach (char c in str)
            {
                brojac = brojac + 1;
                if (brojac != 1)
                {
                    if (c == '-')
                    {
                        provjera = false;
                    }
                }
                if (c == '.')
                {
                    brojactocki = brojactocki + 1;
                }
            }
            if (brojactocki > 1)
            {
                provjera = false;
            }
            foreach (char c in str)
            {
                if ((c < '0' || c > '9') && (c != '.') && (c != '-'))
                {
                    provjera = false;
                }
            }
            double dblString;
            if (deg == 0)
            {
                if (provjera == true)
                {
                    dblString = Convert.ToDouble(str.Replace(".", ","));
                    if (dblString >= 90 || dblString <= -90)
                    {
                        provjera = false;
                    }
                }
            }
            if (deg == 1)
            {
                if (provjera == true)
                {
                    dblString = Convert.ToDouble(str.Replace(".", ","));
                    if (dblString >= 180 || dblString <= -180)
                    {
                        provjera = false;
                    }
                }
            }
            return provjera;
        }

最新更新