Finding exact values associated with given word using regex in python

Viewed 41

am trying to find values associated with a particular word using regex but not getting expected results. I wrote a pattern that is working fine for standard input only and I want to so the same for all sorts of inputs.

What I have now:

string = r'''results on 12/28/2012: WBC=8.110*3, RBC=3.3010*6, Hgb=11.3gm/dL'''

Pattern which I wrote:

re.findall(r'{}=(.*)'.format(detected_word), search_query)[0].split(',')[0]

detected_word is variable where am detecting left side part of equals sign like (WBC, RBC,...) using another technique.

In this above case, it's working fine, but if I change the sentence pattern like below am unable to find a generic pattern.

string = r'''results on 12/28/2012: WBC=8.110*3, RBC=3.3010*6 and Hgb=11.3gm/dL'''
string = r'''results for WBC, RBC and Hgb are 8.110*3, 3.3010*6 and 11.3gm/dL'''

no matter of string format I can able to detect WBC, RBC, and Hgb these words but detecting the value for an associated word is worrying me

Could anyone please help me with this? Thanks in advance

1 Answers

Here is an idea: use two separate patterns for the strings you provided as sample input, the first one will extract values coming after expected word= and the other will extract them from clauses of expected word1 + optional expected word2 + optional expected word3 + "to be" verb + value1, optional value2 and optional value3.

Pattern 1:

\b(WBC|RBC|Hgb)=(\S*)\b

See the regex demo.

  • \b(WBC|RBC|Hgb) - a whole word WBC, RBC or Hgb
  • = - a = char
  • (\S*)\b - Group 2: 0 or more non-whitespaces, that stops at last word boundary position

Pattern 2:

\b(WBC|RBC|Hgb)(?:(?:\s+and)?(?:\s*,)?\s+(WBC|RBC|Hgb))?(?:(?:\s+and)?(?:\s*,)?\s*(WBC|RBC|Hgb))?\s+(?:is|(?:a|we)re|was|will\s+be)(?:\s*,)?\s*(\d\S*)\b(?:(?:\s+and)?(?:\s*,)?\s*(\d\S*)\b)?(?:(?:\s+and)?(?:\s*,)?\s*(\d\S*)\b)?

See regex demo.

  • \b(WBC|RBC|Hgb) - Group 1 capturing the searched word
  • (?:(?:\s+and)?(?:\s*,)?\s*(WBC|RBC|Hgb))? - an optional pattern:
    • (?:\s+and)? - an optional sequence of 1+ whitespaces and then and
    • (?:\s*,)? - an optional sequence of 0+ whitespaces and then a comma
    • \s*(WBC|RBC|Hgb) - 0+ whitespaces and Group 2 capturing the searched word
  • (?:(?:\s+and)?(?:\s*,)?\s*(WBC|RBC|Hgb))? - same as above, captures the 3rd optional searched word into Group 3
  • \s+ - 1+ whitespaces
  • (?:is|(?:a|we)re|was|will\s+be) - a VERB, you may add more if you expect them to be at this position, or plainly try a \S+ or \w+ pattern instead
  • (?:\s*,)?\s* - an optional 0+ whitespaces and a comma sequence, then 0+ whitespaces
  • (\d\S*)\b - Group 4 (pair it with Group 1 value): a digit and then 0+ non-whitespace chars limited by a word boundary
  • (?:(?:\s+and)?(?:\s*,)?\s*(\d\S*)\b)? - an optional group matching
    • (?:\s+and)? - an optional sequence of 1+ whitespaces and and
    • (?:\s*,)?\s* - an optional 0+ whitespaces and a comma, then 0+ whitespaces
    • (\d\S*)\b - Group 5 (pair it with Group 2 value): a digit and then 0+ non-whitespace chars limited by a word boundary
  • (?:(?:\s+and)?(?:\s*,)?\s*(\d\S*)\b)? - same as above, with a capture group 6 that must be paired with Group 3.
Related