Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i write a regex for capturing decimal numbers?

Tags:

regex

I need to write a regex that will capture all of the following numbers from a sample text:

2.5
5
0.2
.5

Assuming its not going to be more than 2 digits on either side of the decimal point, what regex do i use?

thanks.

like image 354
Karthick Avatar asked May 18 '11 03:05

Karthick


1 Answers

This should work.

(\d*\.?\d+)

It means

  • ( begin capture group
  • \d* any digit zero or more times
  • \.? a period zero or one times (i.e. it is optional)
  • \d+ any digit one ore more times
  • ) end capture group

It will match all the number you listed and capture them in $1.

like image 114
Jeff Avatar answered Oct 05 '22 23:10

Jeff