Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help fix Regular expression
#1
I'm attempting to extract the 2 digit value that may appear on either side of the term 'm_m_s_e'

I tested the following expression on regex101.com and the expression works as expected and returns the numerical value I want for a vast majority of my records. I have a few that I can't locate why I have no match when it should.

Looks like I can match values if it appears after m_m_s_e but not if it appears before

The expression finds my score for this string
String1 = clear skin lesions noted mental status, cognition, and cortical functions:m_m_s_e 25/30 · mental status (including orientation to person, place, and time; recent
I get 25


but no match for this one
String2 = examination, she was alert and cooperative she scored 29/30 on the m_m_s_e language was fluent pupils were equal and reacted directly and
I'd expect 29

nor this one

String5 = the hour hand where of the same size she scored 25/30 m_m_s_e she had a cautious gait she has mild postural instability
I'd expect 25

# A function to get the MMSE score from Provider notes.
import re
def get_score(notes):
   
  #  note_search = re.search(' ([A-Za-z]+)\.', name)
   score_search =  re.search(r".*?(?:\b(\d\d)\b.*m_m_s_e|m_m_s_e.*?\b(\d\d)\b).*", notes)
    
    # If the title exists, extract and return it.
   if score_search:
        return score_search.group(2)
   return ""
Reply
#2
Your function is returning group #2. If it matches before m_m_s_e, the match is in group #1.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
you can accomplish the same task using str.split() and the in operator
data = '''clear skin lesions noted mental status, cognition, and cortical functions:m_m_s_e 25/30 · mental status (including orientation to person, place, and time; recent
20/30 blah
examination, she was alert and cooperative she scored 29/30 on the m_m_s_e language was fluent pupils were equal and reacted directly and
the hour hand where of the same size she scored 25/30 m_m_s_e she had a cautious gait she has mild postural instability 
'''

for line in data.split('\n'):
    if 'm_m_s_e' in line:
        for word in line.split():
            if '/' in word:
                print(word.split('/')[0])
Output:
25 29 25
Recommended Tutorials:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  data validation with specific regular expression shaheen07 0 329 Jan-12-2024, 07:56 AM
Last Post: shaheen07
  Regular Expression search to comment lines of code Gman2233 5 1,660 Sep-08-2022, 06:57 AM
Last Post: ndc85430
  List Creation and Position of Continue Statement In Regular Expression Code new_coder_231013 3 1,662 Jun-15-2022, 12:00 PM
Last Post: new_coder_231013
  Need help with my code (regular expression) shailc 5 1,920 Apr-04-2022, 07:34 PM
Last Post: shailc
  Regular Expression for matching words xinyulon 1 2,165 Mar-09-2022, 10:34 PM
Last Post: snippsat
  regular expression question Skaperen 4 2,476 Aug-23-2021, 06:01 PM
Last Post: Skaperen
  How can I find all combinations with a regular expression? AlekseyPython 0 1,670 Jun-23-2021, 04:48 PM
Last Post: AlekseyPython
  Python Regular expression, small sample works but not on file Acernz 5 2,926 Jun-09-2021, 08:27 PM
Last Post: bowlofred
  Regular expression: cannot find 1st number in a string Pavel_47 2 2,413 Jan-15-2021, 04:39 PM
Last Post: bowlofred
  Regular expression: return string, not list Pavel_47 3 2,491 Jan-14-2021, 11:49 AM
Last Post: Pavel_47

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020