3

I'm writing a C++ program to isolate parts of HL7 messages.

I have regex statements that I have tested on regex101's PCRE regex tester, across my sample text, which are messages such as this one:

MSH|^~\&|CERNER||PriorityHealth||||ORU^R01|Q479004375T431430612|P|2.3|
PID|||001677980||SMITH^CURTIS||19680219|M||||||||||929645156318|123456789| PD1||||1234567890^LAST^FIRST^M^^^^^NPI|
OBR|1|341856649^HNAM_ORDERID|000002006326002362|648088^Basic Metabolic Panel|||20061122151600|||||||||1620^Hooker^Robert^L||||||20061122154733|||F|||||||||||20061122140000|
OBX|1|NM|GLU^Glucose Lvl|59|mg/dL|65-99^65^99|L|||F|||20061122154733|

When I test on the tester, I receive matches such as "001677980" or "CURTIS" or "SMITH". However, when using the std::regex_match functionality, I'm getting empty strings when I loop over messages such as these. I have already checked that the messages are correctly being inserted into the messageList vector.

I am getting a correct value for regex A, but I get that same value 5 times, without changing for each message.

I have a function here:

void getBasicPatientInfo(){
        for (int i = 0;i < fileCount;i++) {
            std::string s(messageList[i]);
            std::regex a("PID(?:[^|]*\\|){3}([^|^]*)");
            std::regex b("PID(?:[^|]*\\|){5}(?:[^^]*\\^)([^|^]*)");
            std::regex c("PID(?:[^|]*\\|){5}([^|^]*)");
            std::smatch sma;
            std::smatch smb;
            std::smatch smc;
            std::regex_match (s,sma,a);
            std::regex_match (s,smb,b);
            std::regex_match (s,smc,c);
            int pID;
            std::istringstream inBetween (sma[1]);
            inBetween >> pID;
            std::cout << pID << std::endl;
            patientIDs.push_back(pID);
            patientFNames.push_back(smb[1]);
            patientLNames.push_back(smc[1]);
            messageDates.push_back(smd[1]);
        }
        for (int i = 0;i < fileCount;i++) {
            std::cout << patientIDs[i] << std::endl;
        }
        for (int i = 0;i < fileCount;i++) {
            std::cout << patientFNames[i] << std::endl;
        }
        for (int i = 0;i < fileCount;i++) {
            std::cout << patientLNames[i] << std::endl;
        }
    }

I'd like to know how to correctly use regex to return the parts of the message as I've tested on regex 101.

I'm inserting index 1 into the other vectors, as that is what I have seen elsewhere. Inserting index 0 into the other vectors yields the result that all 15 prints are empty.

Community
  • 1
  • 1
Michael Lee
  • 698
  • 5
  • 13

1 Answers1

1

Regex_search should have been used, not regex_match. The program now outputs the correct parts of the message.

Michael Lee
  • 698
  • 5
  • 13