MC12
MC12

Reputation: 93

pattern match from a string with regex

I am trying to match 2 sets of pattern from text input. From the below example, can I extract numbers which has any of the following character set mg/mcg/ml/g

Meropenem Hospira Powder for Injection (10 Vial of 20 ml) 500mg should match 20 ml and 500mg.

My regex ([\d+\.+\d+]+(mg|g|mcg|ml|)+)+ matches 10 , 20 ml , 500mg

Upvotes: 1

Views: 360

Answers (2)

Regexr
Regexr

Reputation: 56

([\d*.?\d+]+\s*(?:mg|g|mcg|ml)(?![a-z,A-Z])+)

This worked for me. The two problems I noticed with your expression was wrapping the units in () instead of [] and not specifically calling out whitespace characters as a possibility.

This also assumes that the regex you're using can handle non-capture groups (?:)

This will not match 20 g in something like "20 graphites", for example.

Upvotes: 2

user557597
user557597

Reputation:

This should work for you (\d+(?:\.\d*)?|\.\d+)\s*(g|m(?:c?g|l))

https://regex101.com/r/bKMmKi/1

Per Match:
Quantity in group 1
Volume units in group 2

Formatted:

 (                             # (1 start), The decimal amount
      \d+ 
      (?: \. \d* )?
   |  \. \d+ 
 )                             # (1 end)
 \s*                           # Optional whitespace
 (                             # (2 start), The allowed volume units
      g
   |  m
      (?: c?g | l )
 )                             # (2 end)

Upvotes: 0

Related Questions