我想检查输入字符串的格式是否正确。^ [\ d-。] + $表达式只检查数字和。(点)和-(减号)的存在,但我也想检查其顺序。
假设我想将计算器与配合使用。并且-仅。如何获得满足所有条件的正则表达式。
Regex.IsMatch(input, @"^[\d-\.]+$")
//this expression works for below conditions only
if string v1="10-20-30"; // should return true
if string v1="10-20"; // should return true
if string v1="10.20"; // should return true
if string v1="10R20"; // should return false
if string v1="10@20"; // should return false
if string v1="10-20.30.40-50"; // should return true
if string v1="10"; // should return true
//above expression not works for below conditions
if string v1="10--20.30"; // should return false
if string v1="10-20-30.."; // should return false
if string v1="--10-20.30"; // should return false
if string v1="-10-20.30"; // should return false
if string v1="10-20.30."; // should return false
所以像
var pattern = @"^(\d+(-|\.))*\d+$";
应该为您完成工作。
这个正则表达式在说什么:
注意:如果需要使数字为负数,则需要在正则表达式或以下两个\ d +实例之前添加另一个条件减号:
var pattern = @“ ^(-?\ d +(-|。))*-?\ d + $”;
问候
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句