Search code examples
regex-lookarounds

How can I use negative lookbehind to exclude fractions?


I have a list of measurements that need to be deconstructed into quantity (numeric) and unit (string). Things like

1 gal.
500lbs
none
2.25gal
4feet twine
2lbs regular and 2lbs lite

All was well and good using \d+(\.\d+)?, but now I have a fraction thrown into the mix:

3/4gal

I need to exclude the fraction from this search so that I can deal with it separately. I'm successfully excluding the numerator (3) by inserting a negative lookahead-- \d+(?!\/)(\.\d+)?, but I can't figure out how to exclude the denominator (4). I think I'm supposed to use a negative lookbehind but I can't figure out how. \d+(?!\/)(?<!\/)(\.\d+)? and \d+(?!\/)(\.\d+)?(?<!\/) still match the 4.

Thanks!


Solution

  • In a construct like this \d+(?!\/)(?<!\/)(\.\d+)? the lookbehind (?<!\/) is always true as the only thing you can match (not assert) before is a digit.

    You might also exclude a / on the left of the digits part, and add the lookahead after matching the decimal part.

    (?<!/)\d+(?:\.\d+)?(?!/)
    

    Explanation

    • (?<!/) Negative lookbehind, assert directly to the left of the current postion is not /
    • \d+ Match 1+ digits
    • (?:\.\d+)? Match an optional . and 1+ digits
    • (?!/) Negative lookahead, assert directly to the right of the current position is not /

    regex demo