C# 正则表达式限制输入纬度(十进制度数)文本框

C# 正则表达式限制输入纬度(十进制度数)文本框,c#,regex,C#,Regex,我有一个带有文本框的WPF应用程序,用户可以在其中输入十进制度数的纬度值(精度高达7位)。当然,有效纬度范围为-90.0000000到90.0000000。我正在尝试创建一个正则表达式,以通过PreviewTestInput事件限制文本框的输入,类似于: private void latitudeTextBox_PreviewTextInput(object sender, TextCompositionEventArgs e) { e.Handled = !Va

我有一个带有文本框的WPF应用程序,用户可以在其中输入十进制度数的纬度值(精度高达7位)。当然,有效纬度范围为-90.0000000到90.0000000。我正在尝试创建一个正则表达式,以通过PreviewTestInput事件限制文本框的输入,类似于:

    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.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;
}
公共静态bool字符串(字符串小数字符串)
{
if(ValidateDecimalString(小数字符串)){
双lat=0;
返回double.TryParse(小数字符串,out-lat)和&lat=-90;
}
返回false;
}
因此,如果没有类似regex的

public static bool ValidateLatitudeString(string decimalString)
{
    double lat = 0;
    return double.TryParse(decimalString, out lat) && lat<=90.0 && lat>=-90;
}
公共静态bool字符串(字符串小数字符串)
{
双lat=0;
返回double.TryParse(小数字符串,out-lat)和&lat=-90;
}
这个怎么样

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(包括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

为此提供大量选项。单向-

 #  @"^-?(?:(?:[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} | $ )
      )?
 )?
 $

虽然您的问题是如何使用regex验证纬度,但似乎更好的方法是使用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;
    }
公共静态bool字符串(字符串小数字符串)
{
十进制有效度;
if(十进制三分位(十进制字符串,超出有效范围))
{

如果(validLatitude>=-90.0M&&validLatitude另一种方式
^-?[0-8]?\d(?:\.\d*)?\124;-?90(?:\.0+)?$

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

        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;
        }
bool isdigitsonlylitadalongituda(字符串str,int deg)
{
//值为0的度为纬度,值为1的度为经度
bool provjera=true;
int brojactocki=0;
如果(str.Length>0)
{
如果(str[0]='。)
{
provjera=false;
}
如果(str[str.Length-1]='.')
{
provjera=false;
}
}
var-brojac=0;
foreach(str中的字符c)
{
brojac=brojac+1;
如果(brojac!=1)
{
如果(c=='-')
{
provjera=false;
}
}
如果(c=='。)
{
布罗贾托基=布罗贾托基+1;
}
}
如果(brojactocki>1)
{
provjera=false;
}
foreach(str中的字符c)
{
如果((c<'0'| | c>'9')&&(c!='.')&&(c!='-'))
{
provjera=false;
}
}
双dblString;
如果(度==0)
{
如果(provjera==true)
{
dblString=Convert.ToDouble(str.Replace(“.”,“,”);

如果(dblString>=90 | | dblString=180 | | | dblString回答得很清楚。如果你想经常使用这个函数,你可能想把它放在扩展类,
DecimalStringExtender
或其他什么东西中,并将参数改为
this string decimalString
。你还可以通过将
正则表达式存储为一个静态成员变量(<代码>静态只读正则表达式DeimalReGeX=……/代码>)。最后考虑“代码> ReXEXOPTIONS。编译< /代码>以获得额外的性能。与-ReX相关的99999999—Z999 99。有效纬度范围为-90.00万至90=<代码> ValidateDecimalString(“999”);???@?L.B-Tral:regex不应该测试范围,而应该测试格式,以便在regex使用转换和比较后更好地测试范围。您的逻辑建议
+89.
-.0
不在十进制-90和90之间。这些应该通过表单测试,否则,表单应该是唯一的测试。我有一个类似这样的现有验证规则来检查范围我试图用正则表达式实现的是简单地将输入限制到文本框中(因此使用PreviewTestInput事件)。@user685869 Hm在这种情况下,为什么不使用
“^-?[0-9]+([.][0-9]*)?$”进行验证呢
?虽然,请看我的答案,其中大部分数字都被正确检查或完全检查过,但没有什么不同。这很公平。如果您已经在后端以这种方式进行验证,可能只是有一个允许数字的正则表达式。和-。如果他们用99.9之类的东西搞砸了,那么使用后端验证来捕获它。不是这样您无法编写在所有边缘情况下都能工作的长正则表达式。