简体   繁体   English

带有范围结构的python中的正则表达式

[英]regular expression in python with range structure

I need to fetch lower and upper values in a string having structure in a mix of the following formats: 我需要在具有以下格式混合结构的字符串中获取较低和较高值:

Rules:
1. If lower and upper range is available then they are separated by '-'. 
2. Sometimes the range is written as <=xx.y

2a. If 'less than' is anywhere in the text then search for the number. pl. see Example below:

3. If at all age range appears then it appears always before the range, separated from range by a ':'
4. the unit is optional

Example data 示例数据

10.0 - 35.0 MCG/ML
<=6.0 MG/24 H
51-60 YEARS: 37-129
15 - 60
0.5-9.9 %
LESS THAN 30 PG/ML
LESS THAN OR EQUAL 35 UG/DL
LESS THAN OR EQUAL TO 35
NEGATIVE: LESS THAN 20
REF RANGE LESS THAN 2.0
1.3 OR LESS PMOL/L
LAR: LESS THAN 1 NG/M

From the above, sample, my output would be: 从上面的示例中,我的输出将是:

10.0,35.0, MCG/ML
0, 6.0, MG/24 H
37, 129,
15,60
0.5, 9.9, %

Edit: 编辑:

the string is in 'refVal'
re.search(r'([0-9]*\.?[0-9]*)\s*-\s*([0-9]*\.?[0-9]*)', refVal)
re.search(r'(<=|<|<\s*=|<\sOR\s=)\s*([0-9.]+)', refVal)

I added some more examples in the example above (especially for less than). 我在上面的例子中添加了一些例子(特别是少于)。 I want to write Regex that fetch the value if 'Less Than' is in the text. 我想编写Regex,如果文本中有'Less Than',则获取值。

The following gives me unwanted 'None'. 以下给了我不想要的'无'。

>>> re.search(r'([0-9.]+) OR LESS|LESS THAN ([0-9.]+)', '5.4 OR LESS').groups()
('5.4', None)

IMO you aren't going to get a reliable solution with regex alone. IMO你不会单独使用正则表达式获得可靠的解决方案。 If it were me, I'd break it down into multiple conditions and regexes. 如果是我,我会将其分解为多个条件和正则表达式。 Having said that, for shits and grins I did come up with this...it does match for everything above, but it is quite ugly; 话虽如此,对于屎和笑容,我确实提出了这个......它确实与上述所有内容相匹配,但它非常难看; for starters the data is captured to different groups depending on the format... 对于初学者,根据格式将数据捕获到不同的组...

(?(?=.*:).*:\s*([0-9.]+)\s*-\s*([0-9.]+)|(?(?=.*\<=)(.*?)<=\s*([0-9.]+)\s*(.*)|([0-9.]+)\s*-\s*([0-9.]+)\s*(.*)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM