Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex that matches any positive or negative numeric value but no characters or mixed strings [closed]

Tags:

regex

numbers

I haven't used regular expressions till now, but I need a regex that matches a string representing a positive or negative numeric value like

234

-8

3.346

-564.4

It should NOT match any text or mixtures of characters (<>#?_...), numbers and text like

abc

.-.

<11.45

amount111.43

345.654.33

like image 663
wilde wutz Avatar asked Jul 31 '12 11:07

wilde wutz


1 Answers

This should do it:

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

^ start of string

-? minus sign one or zero times

\d+ digits, one or more

(\.\d+)? a dot following by one or more digits, this whole block one or zero times

$ end of string

Also take note of Utkanos comment for your next question on SO :)

like image 117
red-X Avatar answered Oct 19 '22 18:10

red-X