简体   繁体   English

REGEX丢弃浮点数和前导零

[英]REGEX to discard floating numbers and leading zeros

I have a Regex expression which should allow only positive number except zero and floating numbers ^0*[1-9][0-9]*(\\.[0-9]+)?|0+\\.[0-9]*[1-9][0-9]*$ but it is allowing floating numbers, can anyone modify this and let me know the regex 我有一个Regex表达式,除了零和浮点数之外只允许正数^0*[1-9][0-9]*(\\.[0-9]+)?|0+\\.[0-9]*[1-9][0-9]*$但它允许浮动数字,任何人都可以修改这个并让我知道正则表达式

valid: 有效:

1-any number 
01 - any number

not valid: 无效:

00 - any leading zeros
0
1.4 - floating numbers
abc - alphabets 
-1 - negative numbers

Try this pattern [0]*[1-9][0-9]* as: 试试这个模式[0]*[1-9][0-9]*为:

String str = "00012";
str.matches("[0]*[1-9][0-9]*");

Where: 哪里:

[0]* is any number of 0 at start of the string [0]*是字符串开头的任意数字0

[1-9] one digit from 1 to 9 [1-9]从1到9的一位数

and [0-9]* any number of digits from 0 to 9 [0-9]*从0到9的任意位数

If you want to use it with JavaScript, just modify it with ^ and $ to determine the start and the end of the string like: 如果你想在JavaScript中使用它,只需用^和$修改它来确定字符串的开头和结尾,如:

^[0]*[1-9][0-9]*$

Stanislav's regex (see below) I think matches what you want. 斯坦尼斯拉夫的正则表达式(见下文)我认为匹配你想要的。 Upvote him if you agree! 如果您同意,请向他投票! (Note: you may need to wrap it in ^ and $ so it matches the beginning and end of the string, like this: ^[0]*[1-9][0-9]*$ ) You can test it here: (注意:您可能需要将其包装在^和$中,以便它匹配字符串的开头和结尾,如下所示: ^[0]*[1-9][0-9]*$ )您可以在此处测试:

 /* https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Forms/Data_form_validation */ input:invalid { border: 5px solid red; } 
 <input pattern="[0]*[1-9][0-9]*"> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM