Regex for Decimals with One Trailing Digit
# Regex for validating decimals with one trailing digit
To match numbers that are decimals with one trailing digit, such as 0.5 or .5, but not single numbers like 0 or 1, you can use the following regular expression:
### A possible regex
“`
^\d+\.\d{1}$
“`
### Regex Breakdown
“`
^ # Matches the start of the string
\d+ # Matches one or more digits
\. # Matches a literal dot character
\d{1} # Matches exactly one digit
$ # Matches the end of the string
“`
Examples of strings that should match this regular expression are:
– `0.5`
– `.5`
– `123.4`
– `9.0`
Examples of strings that should not match this regular expression are:
– `0`
– `1`
– `12.34`
– `1.23`
### Final notes
This regular expression will only match numbers that have exactly one digit after the decimal point. If you want to allow for more than one trailing digit, you can modify the `\d{1}` part of the regex accordingly.
Read more here: Source link