Search code examples
regexregex-group

Regex pattern for matching float followed by some fixed strings


I want to a regex pattern that could match the following cases:

0, 1, 0.1, .1, 1g, 0.1g, .1g, 1(g/100ml), .1(g/ml)

If the regex matches the pattern, I want to capture only the numerical part(0,1,0.1..)

I tried using following regex but it matches many cases:

((?=\.\d|\d)(?:\d+)?(?:\.?\d*))|((?=\.\d|\d)(?:\d+)?(?:\.?\d*))[a-zA-Z]+?|\([^)]*\)

How to achieve above with single regex pattern?

Edit:

To make the question solution more generic What would be a single regex that would match below

  1. Any numerical ( 0, 1, 0.1, ...)
  2. Any numerical followed by g, mg any characters (0.1g, .1mg, 100kg)
  3. Any numerical followed by anything in parentheses - .1(g/100ml), 100(mg/1kg)

And just capture the numerical part


Solution

  • EDIT2: With OP's edited samples(to match 0, 1, 0.1 OR (0.1g, .1mg, 100kg) OR .1(g/100ml), 100(mg/1kg)), adding following solution here. Explanation is same as very first solution, only thing is in spite of matching specific strings, I have changed regex to match any alphabets here.

    (\d*\.?\d+)(?:[a-zA-Z]+|\([a-zA-Z]+(?:\/\d*(?:[a-zA-Z]+))?\)|(?:,\s+|$))
    

    Online Demo for above regex



    EDIT1: As per OP's comments to match .01c and 100(g/1000L) kind of examples adding following regex, which is small edit to 1st solution here.

    (\d*\.?\d+)(?:g|cc|\(g(?:\/\d*(?:ml|L))?\)|(?:,\s+|$))
    

    Online demo for above regex



    With your shown samples, please try following regex here.

    (\d*\.?\d+)(?:g|\(g(?:\/\d*ml)?\)|(?:,\s+|$))
    

    Online demo for above regex

    Explanation: Adding detailed explanation for above.

    (\d*\.?\d+)           ##Matching digits 0 or more occurrences followed by .(optional, followed by 1 or more digits occurrences here.
    (?:                   ##Starting a non-capturing group here.
      g|                  ##matching only g here OR.
      \(g(?:\/\d*ml)?\)|  ##Matching (g) OR (g/digits ml) here OR.
      (?:,\s+|$)          ##Matching comma followed by 1 or more spaces occurrences OR end of value here.
    )                     ##Closing non-capturing group here.