Reputation: 65
I need to create a regular expression in python where I need to extract the value of the exact match of the string
For Example:
record
LDL PRODUCTNAME 5.0,PRODUCTNAME 65.6,HDL PRODUCTNAME 34.1
Where I need only the value of PRODUCTNAME that has to be extracted(i.e, 65.6) and other parameters like LDL PRODUCTNAME and HDL PRODUCTNAME should not be extracted from the record.
But when i use the below regex to find the values of CHOLESTROL
I'm getting all the values(i.e, 5.0 65.6 34.1)
since it finds the match CHOLESTEROL
.
Regex: Value=^\(?!HDL\s|LDL\)(PRODUCTNAME\d{1}\.\d{1})+$
Upvotes: 0
Views: 772
Reputation: 1395
Check out this
import re
strg = 'LDL PRODUCTNAME 5.0,PRODUCTNAME 65.6,HDL PRODUCTNAME 34.1'
match = re.findall(r'((?!PRODUCTNAME\s)\d*\.\d*(?!,)?)', strg)
print ','.join(match)
or
import re
strg = 'LDL PRODUCTNAME 5.0,PRODUCTNAME 65.6,HDL PRODUCTNAME 34.1'
Productname = 'PRODUCTNAME'
match = re.findall(r'' + re.escape(Productname) + ' (\d*\.\d*(?!,)?)', strg)
output = ','.join(match)
print "{} = {}".format(Productname,output)
Upvotes: 0
Reputation: 792
import re
drug_str = 'LDL CHOLESTEROL 5.0,DIMEDROL 65.6,HDL SEMISTEROL 34.1'
search_drug = 'DIMEDROL'
result = re.search(re.escape(search_drug) + r' ([\d.]+)\b', drug_str)
result.group(1)
Upvotes: 0
Reputation: 92854
With the following approach you are able to extract the number parameter of the certain product with or without preceded prefix (LDL|HDL)
:
drug_str = 'LDL CHOLESTEROL 5.0,DIMEDROL 65.6,HDL SEMISTEROL 34.1'
search_drug = 'DIMEDROL'
result = re.search(r'(LDL|HDL)?\s?'+ re.escape(search_drug) + r' ([\d.]+)\b', drug_str)
print(result.group(2))
The output:
65.6
Upvotes: 0
Reputation: 2817
You can use split
:
s = 'LDL PRODUCTNAME 5.0,PRODUCTNAME 65.6,HDL PRODUCTNAME 34.1'
params = dict([item.rsplit(' ', 1) for item in s.split(',')])
print params.get('PRODUCTNAME')
Upvotes: 1