Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 十进制数的正则表达式_Regex_Decimal - Fatal编程技术网

Regex 十进制数的正则表达式

Regex 十进制数的正则表达式,regex,decimal,Regex,Decimal,是否有人能为长度必须介于1和17之间的数字提供正则表达式,并且可以选择包含最多4个位置的尾数?17的长度包括特征和尾数 编辑: 17的长度不包括小数点 有效例子: 12345678901234567 1234567890123.4567 123456789012345.67 12.34 12345678901234567 1234567890123.4567 123456789012345.67 12.34 无效: 12345678901234.5678 (Length of numeral

是否有人能为长度必须介于1和17之间的数字提供正则表达式,并且可以选择包含最多4个位置的尾数?17的长度包括特征和尾数

编辑:

17的长度不包括小数点

有效例子:

12345678901234567 1234567890123.4567 123456789012345.67 12.34 12345678901234567 1234567890123.4567 123456789012345.67 12.34 无效:

12345678901234.5678 (Length of numerals = 18) 12345678901344.5678(数字长度=18)
谢谢。

好的,这是我能做的最好的了:

/^\d{1,17}$|(?=^.{1,18}$)^\d+\.\d{1,4}$/

基本上,匹配1-17个数字,或长度为1-18的字符串,由两组以句点分隔的数字组成。右侧集合只能包含1-4位数字。

不要在正则表达式中完全这样做。在大多数编程语言中,这个问题几乎变得微不足道,这样您就更容易编写、验证、测试和维护。当然,您仍然可以在部分解决方案中使用正则表达式,但您不必这样做。伪代码:

m = re.match(r"(?P<before>[0-9]+)(?P<after>\.[0-9]{1,4})?$", input_string)
if not m:
  return "no match"
before, after = m.group("before", "after")
after = after[1:] if after else ""  # remove period or set to empty string
if len(before) + len(after) > 17:
  return "incorrect length"
return "valid"
m=re.match(r“(?P[0-9]+)(?P\[0-9]{1,4})?$”,输入字符串)
如果不是m:
返回“不匹配”
前、后=m组(“前”、“后”)
after=after[1:]如果在else“”之后#删除句点或设置为空字符串
如果len(前)+len(后)>17:
返回“长度不正确”
返回“有效”

在您最喜欢的语言中,您可以执行一些逻辑检查,例如Python

num="1234567890133.3456"
if "." in num and len(num)==17 :
     n=num.split(".")
     if len(n[1])>4:
         print "cannot have more than 4 decimal places"
     elif len(n)==2 and n[0].isdigit() and n[1].isdigit():
         print "yes, decimal"
elif len(num)==17 and num.isdigit():
     print "%s is number with no decimal and is exactly 17 digits." % num
else:
     print "%s not ok, check length is 17" % num

它不是特别漂亮,但由于可能性太少(0,1,2,3,4长度尾数),我可能只列出它们:

\d{17}|\d{16}\.\d{1}|\d{15}\.\d{2}|\d{14}\.\d{3}|\d{13}\.\d{4}
Regex解释说:

^\d{17}$    //A string of 17 digits 
|           //or
^\d{13}     //13 digits followed by
(?=.{5}$)   //5 characters, of which 
\d*\.\d*    //one is a decimal point and others are digits
\d$         //and the last one is a digit

我已经从以上伟大的解决方案中创建了这个正则表达式。愿它对任何人都有帮助。如果您发现任何错误,请告诉我。
字符串decimalRegex=”“+ “^(?!0[\d,])\+?”+/^数字的开头
(\d{0,“+size+”}|“+//不带组符号的数值|(或)
“(\d{0,+rem(大小,组大小)+”},)?”+
“(\d{0,“+groupSize+”},){0,“+div(size,groupSize)+”}\d{“+groupSize+”})”+//带组符号的数值
“(([a-zA-Z]{0,2}\\”)\s?\+?)\\”+
(\d{0,“+scale+”})?“+//不带组符号的十进制值
“(\s?([a-zA-Z]{0,2}{124;\”{124;\)$”/”以

private int rem(int size,int groupSize ){
    int rem = (size - groupSize)%groupSize;
    return rem;
}

private int div(int size,int groupSize ){
    int div = (size - groupSize)/groupSize;
    return div;
}

你能举一些例子吗。17长度是否也包括小数点?不包括小数点。有效示例:12345678901234567134567 1234567890123.4567 12345678901345.67无效:1234567890134.5678(数字长度=18)@PR:最好使用以下类型的信息更新您的问题:)它匹配1234567890.1234567,您需要在1到4位数字的末尾添加$,它与该字符串不匹配。如果“1234567890.1234567”=~/(^\d{17}$)|((?=^.{18}$)(\d+\.\d{1,4}))/,是否要再次检查?perl-e'print“matched\n”啊,明白了。我正在使用Java。你说得对。试试新版本。不,不行。我也试着这样修改它:\d{1,17}\d{1,16}\.\d{0,1}\d{1,15}.\d{0,2}\d{1,14}.\d{0,3}\d{1,13}.\d{0,4}你用哪种语言来做这个,或者正则表达式是什么?这一个对我有效,但它将取决于解释器。我使用的是ASP.NET RegexValidator控件。谢谢Amarghosh,这就成功了。尽管如此,由于12.34失败了,我不得不对它进行一些调整。以下是我所做的:^\d{1,17}$|^\d{1,13}(?=.{1,5}$)\d*\.\d*\d$谢谢。@PR最初的问题是关于
“一个长度必须为17的数字”
-我在你编辑它之前发布了这个,使它在1到17之间”是的,我的错误。我应该更清楚地回答我的问题。谢谢。:)
private int rem(int size,int groupSize ){
    int rem = (size - groupSize)%groupSize;
    return rem;
}

private int div(int size,int groupSize ){
    int div = (size - groupSize)/groupSize;
    return div;
}