2

I have created a bulky .net regex expression that works, but is quite inefficient. I'd have to assume that there is a more condensed way to write it, but I can't seem to find one.

What I need it to do:

  • match positive and negative integers
  • match positive and negative decimals with or without any numbers preceding the decimal
  • work for .NET since it is being used in a WPF application

Examples of matches from arbitrary example strings:

  • "7" from "7in"
  • "-4" from "-4 mm"
  • ".345" from " .345"
  • "-.43" from " -.43 cm"
  • "0.16" from "$0.16"
  • "-10.04" "-10.04"

What I need to avoid:

  • matching the second number in a range as a negative
    *ex: matching "-3500" in "1000-3500 RPM" (should match 1000 and 3500)

My regex:

(?<![0-9])(-)\.\d+|(?<![0-9])(-)\d+\.\d+|(?<![0-9])(-)\d+|\.\d+|\d+\.\d+|\d+

The Breakdown:

?<![0-9])(-)\.\d+ - a negative decimal without numbers before the decimal and then make sure there is no number before the dash

OR

(?<![0-9])(-)\d+\.\d+ - a negative decimal with numbers before the decimal and then make sure there is no number before the dash

OR

?<![0-9])(-)\d+ - a negative integer without a number preceding the dash

OR

\.\d+ - find a positive decimal with no preceding numbers

OR

\d+\.\d+ - find a positive decimal with preceding numbers

OR

\d+ - find an positive integer

Now, from what I understand, this should be able to be combined into fewer "OR" segments. Id guess that you can at least combine the positive and negative cases and probably the optional numbers proceeding a decimal. Any increased performance would be much appreciated.

Thanks.

Scott P
  • 88
  • 1
  • 9

3 Answers3

0

How about this one: -?(\d*\.)?\d+ ?

PoByBolek
  • 3,405
  • 2
  • 16
  • 22
0

Try (?<!\d)\s*-?\d*(\.\d+)?

Uses a negative lookbehind to assert there are no digits before the dash, also only allows only one decimal point (if any).

Song Gao
  • 656
  • 4
  • 14
0

Looks like this does the job: (?<![0-9])-?\.?([0-9]+)?\.?[0-9]+

Tested here

edit- Condensed slightly: (?<![0-9])-?\.?[0-9]*\.?[0-9]+

Tested here

sharktamer
  • 126
  • 8