I'm working with a dataframe with some medicines and I want extract the dosage from a full sentence taken from the product description.
some examples:
'Anakinra 100 g, gentechnologisch hergestellt aus E. coli.'
'Anakinra 100 mg, gentechnologisch hergestellt aus E. coli.'
'Anakinra 10.5 g, gentechnologisch hergestellt aus E. coli.'
'Anakinra 10, gentechnologisch hergestellt aus E. coli.'
I want to have:
'100g'
'100mg'
'10.5g'
'10'
Because I want to do this to every product, I decided to use a regex with the product's name as a variable, so I can later run a cycle for the full list of products.
I tried:
a_string = "Anakinra 100 mg, gentechnologisch hergestellt aus E. coli."
pattern = 'Anakinra'
re.findall(f"({pattern}\s*\d+(?:[.,]\d+)*\s*\b(g|mg|)", a_string)
#[('Anakinra 100 mg', 'mg')]
As you can see it's returning two groups instead of just one. This might not be the right procedure either because in the end I only want the dosage part of the string. What would be your solution?