regex to get value and it's proper unit

Question:

i use the following regex to extract values that appear before certain units:

([.d]+)s*(?:kg|gr|g)

What i want, is to include the unit of that specific value
for example from this string :

"some text 5kg another text 3 g more text 11.5gr end"

i should be getting :

["5kg", "3 g", "11.5gr"]

can’t wrap my head on how to modify the above expression to get the wanted result.
Thank you.

Asked By: oussama

||

Answers:

import re

p = re.compile('(?<!d|.)d+(?:.d+)?s*?(?:gr|kg|g)(?!w)')
print(p.findall("some text 5kg another text 3 g more text 11.5gr end"))
Answered By: Ricardo

Other solution (regex demo):

(?i)bd+.?d*s*(?:kg|gr?)b
  • (?i) – case insensitive
  • b – word boundary
    • d+.?d* – match the amount
    • s* – any number of spaces
    • (?:kg|gr?) – match kg, g or gr
  • b – word boundary

import re

p = re.compile(r"(?i)bd+.?d*s*(?:kg|gr?)b")
print(p.findall("some text 5kg another text 3 g more text 11.5gr end"))

Prints:

['5kg', '3 g', '11.5gr']
Answered By: Andrej Kesely
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.