I want to extract certain digits from a string. The problem is that the string can contain the digits in two different patterns. How can I create a regex pattern in re.search such that I can have both patterns to search in a single string?
For e.g.,
## extract 65.45 from this string
string = '1112 (65.45%)'
So, if I do the following, it works
re.search('.*?\((.*)%\)', string).group(1)
and I get the expected result 65.45.
Now, I have another kind of string in the same text that I need to look for.
## from this string, extract 4.00 which appears before [
string = '4.00 [3.00 - 4.50]'
re.search('^(\S+)\s\[.*', string).group(1)
gives me the desired result: 4.00
But if I combine them like the following, it only extracts the one that matches first.
re.search('^(\S+)\s\[.*|.*?\((.*)%\)', string).group(1)
in which case, only the string that contains the square bracket extracts the value, not if the string has a % sign. How can I fix this?
For e.g., If I have a list of strings like the following:
['73 (1.40%)', '38 (1.55%)', '27 (2.17%)', '32 (1.46%)', '10 (1.46%)', '11 (1.04%)', '11 (1.41%)', '7 (1.34%)', '4 (1.24%)', '28 (1.27%)', '750 (14.41%)', '381 (15.54%)', '182 (14.60%)', '313 (14.27%)', '4.10 [3.73 - 4.45]', '4.08 [3.70 - 4.42]', '4.13 [3.77 - 4.47]', '4.13 [3.78 - 4.47]', '4.07 [3.70 - 4.42]', '4.07 [3.70 - 4.43]', '4.07 [3.70 - 4.40]', '4.09 [3.73 - 4.42]', '4.03 [3.63 - 4.40]', '4.10 [3.70 - 4.47]']
I want to do certain things with each value that is extracted and compare with a specific threshold value.
Using the for-loop, I did something like this:
for val in string:
match = re.search('^(\S+)\s\[.*|.*?\((.*)%\)', val)
print(match)
which results in the following:
<re.Match object; span=(0, 10), match='73 (1.40%)'>
<re.Match object; span=(0, 10), match='38 (1.55%)'>
<re.Match object; span=(0, 10), match='27 (2.17%)'>
<re.Match object; span=(0, 10), match='32 (1.46%)'>
<re.Match object; span=(0, 10), match='10 (1.46%)'>
<re.Match object; span=(0, 10), match='11 (1.04%)'>
<re.Match object; span=(0, 10), match='11 (1.41%)'>
<re.Match object; span=(0, 9), match='7 (1.34%)'>
<re.Match object; span=(0, 9), match='4 (1.24%)'>
<re.Match object; span=(0, 10), match='28 (1.27%)'>
<re.Match object; span=(0, 12), match='750 (14.41%)'>
<re.Match object; span=(0, 12), match='381 (15.54%)'>
<re.Match object; span=(0, 12), match='182 (14.60%)'>
<re.Match object; span=(0, 12), match='313 (14.27%)'>
<re.Match object; span=(0, 18), match='4.10 [3.73 - 4.45]'>
<re.Match object; span=(0, 18), match='4.08 [3.70 - 4.42]'>
<re.Match object; span=(0, 18), match='4.13 [3.77 - 4.47]'>
<re.Match object; span=(0, 18), match='4.13 [3.78 - 4.47]'>
<re.Match object; span=(0, 18), match='4.07 [3.70 - 4.42]'>
<re.Match object; span=(0, 18), match='4.07 [3.70 - 4.43]'>
<re.Match object; span=(0, 18), match='4.07 [3.70 - 4.40]'>
<re.Match object; span=(0, 18), match='4.09 [3.73 - 4.42]'>
<re.Match object; span=(0, 18), match='4.03 [3.63 - 4.40]'>
<re.Match object; span=(0, 18), match='4.10 [3.70 - 4.47]'>
But not sure how to extract the exact value.
I have to do the .group() to extract the value, but it requires me to know the exact location. And I'm struggling to figure out how to do that.
If I do match.group(2), then I get the following result:
1.40
1.55
2.17
1.46
1.46
1.04
1.41
1.34
1.24
1.27
14.41
15.54
14.60
14.27
None
None
None
None
None
None
None
None
None
None