简体   繁体   English

我将如何更改此正则表达式以在末尾接受0.5的单个十进制?

[英]How would I change this regular expression to accept a single decimal of .5 at the end?

I have the following regex: 我有以下正则表达式:

/^(([0-2][0-9]{2}|3[0-5][0-9])|360)$/

This accepts numbers from 000-360. 这接受从000-360的数字。 I also want it to accept the decimal .5 at the end. 我也希望它最后接受十进制.5。 No other decimal should be accepted and it should also accept no decimal at all. 不应接受任何其他十进制,也不应接受任何十进制。 Also 360.5 should not be accepted. 同样,不应接受360.5。 How would I do this? 我该怎么做?

/^(([0-2][0-9]{2}|3[0-5][0-9])|360)(|\.5)$/

How about: 怎么样:

^((([0-2][0-9]{2}|3[0-5][0-9])(\.5)?)|360)$

https://regex101.com/r/gQ1vO6/2 https://regex101.com/r/gQ1vO6/2

在行尾之前的末尾添加(\\.5)*

Assuming 000 <= x <= 360 , but allowing for .5 values within that range: 假设000 <= x <= 360 ,但允许该范围内的.5值:

^((?:[0-2][0-9]{2}|3[0-5][0-9])(?:\.5)?|360)$

I've added a non-capturing group to target 000 - 359 and added an optional .5 group after that. 我添加了一个非捕获组目标000 - 359 ,并添加一个可选的.5之后该组。

^                                # Start of string
 (                               # Begin capture
  (?:[0-2][0-9]{2}|3[0-5][0-9])    # 000-359
  (?:\.5)?                         # (optional) .5 suffix
  |                              # -- OR --
  360                              # 360 (explicitly)
 )                               # End capture
$                                # End of string

This will also make the first capture group record whatever the number is. 这也将使第一个捕获组记录任何数字。

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

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