Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegEx for both, integer and float [closed]

Tags:

regex

I need a regex to match both, integer values aswell as float numbers (whereas float numbers have a "." as seperator). Those numbers are always in a bracket and may have a leading "+".

What should be valid:

  • (1.0)
  • (1)
  • (9.9)
  • (10000000)
  • (+15)

What should be invalid:

  • 1.0 --- because no bracket
  • 5 --- because no bracket
  • (1,5) --- becaue "," instead of "."
  • (a) --- because of not a number
  • (1 5) --- because of not only one number
  • (1+5) --- because... well... just failing the pattern
  • [5] --- because wrong brackets
like image 836
user2015253 Avatar asked Jan 27 '13 18:01

user2015253


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 does (? I do in regex?

(? i) makes the regex case insensitive. (? c) makes the regex case sensitive.

How do you match a floating point number in regex?

[0-9]+|[0-9]+). This regular expression matches an optional sign, that is either followed by zero or more digits followed by a dot and one or more digits (a floating point number with optional integer part), or that is followed by one or more digits (an integer).

How do I match a pattern in regex?

To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" . You also need to use regex \\ to match "\" (back-slash).


1 Answers

This should work on most perl like regex engines:

/(\d+(?:\.\d+)?)/
like image 111
ennuikiller Avatar answered Oct 16 '22 00:10

ennuikiller