java 什么正则表达式将匹配 4 位数字、一个句点,然后匹配一位且仅一位?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4511766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 06:41:26  来源:igfitidea点击:

What regex will match 4 digits, a period, then one and only one digit?

javaregex

提问by Andrew Clark

I need a (Java) regular expression that will match:

我需要一个(Java)正则表达式来匹配:

XXXX.X

Where X is any number, only one number after the decimal point.

其中 X 是任意数字,小数点后只有一个数字。

回答by Andrew Clark

Try ^\d{4}\.\d$if you want the entire string to match, remove the ^and/or $if you want it to find matches within a larger string.

^\d{4}\.\d$如果您希望整个字符串匹配,请尝试删除^和/或$如果您希望它在更大的字符串中查找匹配项。

If there can be any number of integers before the .use \d+instead of \d{4}to match one or more, or \d*to match zero or more (the string ".5"would match \d*\.\d).

如果在.使用之前可以有任意数量的整数,\d+而不是\d{4}匹配一个或多个,或者\d*匹配零个或多个(字符串".5"将匹配\d*\.\d)。

回答by sujithayur

If the number is exactly 4 digits,then try this

如果数字正好是 4 位数字,那么试试这个

"/(^([0-9]{4})[.]([0-9]{1})$)/"

Eg : 1234.4

例如:1234.4

Or if the number is of unlimited digits,try this..

或者如果号码是无限位数,试试这个..

"/(^([0-9]{0,})[.]([0-9]{1})$)/"

Eg: 1234.4
45.8
589745324744.7

例如:1234.4
45.8
589745324744.7