1

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

12
  • What exactly is the expected output? Commented Jan 25, 2018 at 18:50
  • output should be only the digits that are followed by mg/mcg/ml/g like 20 ml and 500mg Commented Jan 25, 2018 at 18:52
  • Do you really want potentially multiple decimal points? Commented Jan 25, 2018 at 18:52
  • 1
    What about \d+(?:\.\d+)?(?=\s*(?:m?g|mcg|ml)\b)? Commented Jan 25, 2018 at 18:55
  • 1
    You can use either \d+(?:\.\d+)?(?=\s*(m?g|mcg|ml)\b) or (\d+(?:\.\d+)?)\s*(m?g|mcg|ml)\b Commented Jan 25, 2018 at 19:01

2 Answers 2

2

([\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.

Sign up to request clarification or add additional context in comments.

2 Comments

The above regex has the output (3 c Nivestim (3 concetrations) For this example it should not match at all
Edited my answer above.
0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.