Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegEx Pattern that matches positive or negative values (e.g "1.2", "-2.8", "7.8", -22.8")

Tags:

java

regex

decimal seperator is a dot, followed by max one digit! No range specified.

Thanks guys!

like image 763
tzippy Avatar asked May 18 '10 10:05

tzippy


People also ask

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string). Both are called anchors and ensure that the entire string is matched instead of just a substring.

What is a regex give an example of a regex pattern?

A regex (regular expression) consists of a sequence of sub-expressions. In this example, [0-9] and + . The [...] , known as character class (or bracket list), encloses a list of characters. It matches any SINGLE character in the list.

What is regex matching pattern?

A regex pattern matches a target string. The pattern is composed of a sequence of atoms. An atom is a single point within the regex pattern which it tries to match to the target string. The simplest atom is a literal, but grouping parts of the pattern to match an atom will require using ( ) as metacharacters.


2 Answers

^-?\d+(\.\d)?$

if the decimal part is optional, and

^-?\d+\.\d$

if it's required :)

like image 183
Artiom Chilaru Avatar answered Oct 04 '22 22:10

Artiom Chilaru


Simple: -?\d+\.\d

like image 20
ZyX Avatar answered Oct 04 '22 21:10

ZyX