0

I want to build a regex that will extract me numbers from a string. The pattern is

">number<"

Now the number can have decimals or not. I went with:

"[^\d]+"

This does extract the numbers but because of decimals, it sometimes works bad. Any ideas?

Sogard N
  • 91
  • 9
  • Welcome to StackOverflow. Is this similar question of any help by any chance? http://stackoverflow.com/questions/308122/simple-regular-expression-for-a-decimal-with-a-precision-of-2 – José Luis Dec 03 '16 at 21:10

4 Answers4

0

Try this (copy with the quotes):

">[0-9]+(\.[0-9]+)?<"
Dimitar Nikovski
  • 825
  • 10
  • 15
0

A simple regex which works for integers, floats and negative numbers :

>([\+\-]?\d+\.?\d*)<

The number is in group 1.

If you can use positive lookarounds, this regex matches just a number between >< and nothing else :

(?<=>)[\+\-]?\d+\.?\d*(?=<)

Here in action.

Eric Duminil
  • 48,038
  • 8
  • 56
  • 100
0
>((\-|\+)?[0-9]+(\.[0-9]+)?)<

Explained:

  • (\-|\+)? optional sign: - or +
  • [0-9]+ - non-empty sequence of digits
  • (\.[0-9]+)? - optional sequence starting with a dot followed by non-empty sequence of digits
  • > and < at the beginning and at the and (not in the matching group)

In the first matching group you will have your number.

Example here.

Adam
  • 4,781
  • 6
  • 27
  • 35
0

Assuming that it's just the number you want:

(?<=\\>)[0-9]+(\.[0-9]+)?(?=\<)

It matches any number with or without decimals between > and < but excluding > and <

Nimantha
  • 4,731
  • 5
  • 15
  • 38
Jeffrey
  • 1,480
  • 1
  • 17
  • 21