Using re.search multiple times inside for loop to

2020-04-30 16:33发布

I want to retrieve all percentage data as well as integer/float numbers with units from an input text, if it is present in the text. If both are not present together, I want to retrieve atleast the one that is present. Till now if there is an integer/float with an unit in the extracted text, it comes in the result variable.

result=[]
    newregex = "[0-9\.\s]+(?:mg|kg|ml|q.s.|ui|M|g|µg)"
    percentregex = "(\d+(\.\d+)?%)"
    for s in zz:
        for e in extracteddata:
            v = re.search(newregex,e,flags=re.IGNORECASE|re.MULTILINE)
            xx = re.search(percentregex,e,flags=re.IGNORECASE|re.MULTILINE)
            if v:

                if e.upper().startswith(s.upper()):
                    result.append([s,v.group(0), e])
            else:
                if e.upper().startswith(s.upper()):
                    result.append([s, e])

In the code above, newregex identifies numbers/float with an unit after it, percentregex identifies percentage data, zz and extracteddata are as follows

zz = ['HYDROCHLORIC ACID 2M', 'ROPIVACAINE HYDROCHLORIDE MONOHYDRATE', 'SODIUM CHLORIDE', 'SODIUM HYDROXIDE 2M', 'WATER FOR INJECTIONS']

extracteddata = ['Ropivacaine hydrochloride monohydrate for injection (corresponding to 2 mg Ropivacaine hydrochloride anhydrous) 2.12 mg Active ingredient Ph Eur ', 'Sodium chloride for injection 8.6 mg 28% Tonicity contributor Ph Eur ', 'Sodium hydroxide 2M q.s. pH-regulator Ph Eur, NF Hydrochloric acid 2M q.s. pH-regulator Ph Eur, NF ', 'Water for Injections to 1 ml 34% Solvent Ph Eur, USP The product is filled into polypropylene bags sealed with rubber stoppers and aluminium caps with flip-off seals. The primary container is enclosed in a blister. 1(1)']

Now I also want to add the condition to extract percentage data in the result variable if it is present but I am stuck with the looping aspect. i want help on using the variable 'xx' to add percentage data to result list if it is present, along with the integer/float numbers with units.

Any help on this.

Updates on attempts made:

result = []
    mg = []
    newregex = "[0-9\.\s]+(?:mg|kg|ml|q.s.|ui|M|g|µg)"
    percentregex = "(\d+(\.\d+)?%)"
    print(type(newregex))
    for s in zz:
        for e in extracteddata:
            v = re.search(newregex,e,flags=re.IGNORECASE|re.MULTILINE)
            xx = re.search(percentregex,e,flags=re.IGNORECASE|re.MULTILINE)
            if v:
#                mg.append(v.group(0))

                if e.upper().startswith(s.upper()):
                    result.append([s,v.group(0), e])
            elif v is None:
                if e.upper().startswith(s.upper()):
                    result.append([s, e])
            elif xx:
                if v:
                    if e.upper().startswith(s.upper()):
                        result.append([s,v.group(0),xx.group(0), e])
            elif v is None:
                if  xx:
                    if e.upper().startswith(s.upper()):
                        result.append([s,xx.group(0), e])
            elif v is None and xx is None:
                if e.upper().startswith(s.upper()):
                        result.append([s, e])
            else:
                print("DOne")

1条回答
对你真心纯属浪费
2楼-- · 2020-04-30 16:56

Here is a Python demo of what we talked about in the comments :

mod per request

>>> import re
>>> 
>>> extracteddata = ['"Water 5.5 ml for injections 0.80 and 100 at 2.2 % ','Injections 100 and 0.80', 'Ropivacaine hydrochloride monohydrate for injection (corresponding to 2 mg Ropivacaine hydrochloride anhydrous) 2.12 mg Active ingredient Ph Eur ', 'Sodium chloride for injection 8.6 mg 28% Tonicity contributor Ph Eur ', 'Sodium hydroxide 2M q.s. pH-regulator Ph Eur, NF Hydrochloric acid 2M q.s. pH-regulator Ph Eur, NF ', 'Water for Injections to 1 ml 34% Solvent Ph Eur, USP The product is filled into polypropylene bags sealed with rubber stoppers and aluminium caps with flip-off seals. The primary container is enclosed in a blister. 1(1)']
>>> 
>>> Rx = r"(?i)(?=.*?((?:\d+(?:\.\d*)?|\.\d+)\s*(?:mg|kg|ml|q\.s\.|ui|M|g|µg)))?(?=.*?(\d+(?:\.\d+)?\s*%))?(?=.*?((?:\d+(?:\.\d*)?|\.\d+))(?![\d.])(?!\s*(?:%|mg|kg|ml|q\.s\.|ui|M|g|µg)))?.+"
>>> 
>>> for e in extracteddata:
...         match = re.search( Rx, e )
...         print("--------------------------------------------")
...         if match.group(1):
...                 print( "Unit num:  \t\t", match.group(1) )
...         if match.group(2):
...                 print( "Percentage num:  \t", match.group(2) )
...         if match.group(3):
...                 print( "Just a num:  \t\t", match.group(3) )
... 
--------------------------------------------
Unit num:                5.5 ml
Percentage num:          2.2 %
Just a num:              0.80
--------------------------------------------
Just a num:              100
--------------------------------------------
Unit num:                2 mg
--------------------------------------------
Unit num:                8.6 mg
Percentage num:          28%
--------------------------------------------
Unit num:                2M
--------------------------------------------
Unit num:                1 ml
Percentage num:          34%
Just a num:              1

This is the regex expanded

 (?i)
 (?=
      .*? 
      (                             # (1 start)
           (?:
                \d+ 
                (?: \. \d* )?
             |  \. \d+ 
           )
           \s* 
           (?: mg | kg | ml | q \. s \. | ui | M | g | µg )
      )                             # (1 end)
 )?
 (?=
      .*? 
      (                             # (2 start)
           \d+ 
           (?: \. \d+ )?
           \s* %
      )                             # (2 end)
 )?
 (?=
      .*? 
      (                             # (3 start)
           (?:
                \d+ 
                (?: \. \d* )?
             |  \. \d+ 
           )
      )                             # (3 end)
      (?! [\d.] )
      (?!
           \s* 
           (?: % | mg | kg | ml | q \. s \. | ui | M | g | µg )
      )
 )?
 .+ 

As seen it uses three look ahead assertions to find the first instances
of the unit and percentage numbers and stand alone numbers.
All values are unique and not an overlap.

Testing each one for non-empty shows if it found that item(s) in the line.

查看更多
登录 后发表回答