Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression to extract (only) the first numeric value

Tags:

java

regex

How could I get only the first number from the strings below?
Regular expression should stop at space or first non-numeric character.

Examples:
6x2mL
7 x 5mL
100Subunits
2*5Kg

like image 532
Louis Papaloizou Avatar asked Feb 13 '23 09:02

Louis Papaloizou


1 Answers

This will help you

String regEx = "^(\d+)";

OR

String regEx = "^([0-9]+)";

It will extract the first numeric value from given Strings.

Output :

6
7
100
2

Same problem is explained here

like image 155
Naveen Kumar Alone Avatar answered Feb 15 '23 09:02

Naveen Kumar Alone