Link to home
Start Free TrialLog in
Avatar of tmajor99
tmajor99

asked on

Regular Expression - Extracting numeric decimal and Integer from string

I need help with regx expression below.  Currently it will only find numeric decimal values but I need both a integer check and numeric decimal check.  

.*?([\-\+]?(\d+\.\d+KW|\d+|\.\d+))\D*

For example:

Input Data                                                            Result
Dual Rated - 240V/3.5KW & 208V/2.5KW    = 3.5KW   --> this works now.
Dual Rated - 240V/5KW & 208V                    = 5KW      --> Expression above does not detect integer values.
5KW                                                                   = 5KW      --> Expression above does not detect integer values.
5.6KW                                                                = 5,6KW
0.0                                                                      = [null]
5                                                                          = [null]
ASKER CERTIFIED SOLUTION
Avatar of wilcoxon
wilcoxon
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
What about this?
\d+(\.\d{1})?KW

Open in new window

That regex has a few issues:
  • missing the capture
  • missing the optional sign
  • will only pick up a single decimal digit

The only real reason to keep the beginning .*? is if the regex needs to be anchored to the beginning for some reason.