十进制或小数的正则表达式

编程入门 行业动态 更新时间:2024-10-23 09:36:13
本文介绍了十进制或小数的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

现在我有一个正则表达式,它在其中检查字符串是否为十进制格式:

Right now I have a regex where it checks if the string in a decimal format or not:

-?\\d+(?:.\\d+)//any positive or negative number, including decimals

如果我想为一个分数创建一个正则表达式,它也可以由小数组成,例如:

If I wanted to create a regex for a fraction, which may also consist of decimals, such as:

  • 3.1/2.6
  • 1.00/5.00

以下内容有效吗?

-?\\(d+(?:.\\d+)|d+(?:.\\d+)/d+(?:.\\d+))//a decimal OR a fraction(including decimals)

推荐答案

您可以使用此正则表达式来支持正/负整数,带可选小数部分的小数:

You can use this regex to support positive/negative integers, decimals with optional fractional part:

-?\\b\\d+(?:\\.\\d+)?(?:/\\d+(?:\\.\\d+)?)?\\b

RegEx演示

或者,如果您使用Java的 String#matches()方法,请使用此正则表达式:

Or if you're using Java's String#matches() method then use this regex:

-?\\d+(?:\\.\\d+)?(?:/\\d+(?:\\.\\d+)?)?

更多推荐

十进制或小数的正则表达式

本文发布于:2023-11-06 06:32:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1562946.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:小数   正则表达式   十进制

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!