已检查匹配项时接收到C++正则表达式空字符串

C++ regex empty strings received on checked matches

本文关键字:C++ 正则表达式 字符串 检查      更新时间:2023-10-16

我正在编写一个C++程序来隔离HL7消息的部分。

我在regex101的PCRE regex测试仪上测试了一些regex语句,这些语句在我的示例文本中都是这样的消息:

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|

当我在测试器上测试时;001677980";或";CURTIS";或";史密斯";。然而,当使用std::regex_match功能时,当我循环处理这样的消息时,会得到空字符串。我已经检查了消息是否正确插入到messageList向量中。

我得到了正则表达式a的正确值,但我得到了5次相同的值,每次消息都没有更改。

我在这里有一个功能:

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;
}
}

我想知道如何正确地使用regex来返回消息的部分,正如我在regex 101上测试的那样。

我将索引1插入到其他向量中,这是我在其他地方看到的。将索引0插入到其他向量中会产生所有15个打印都为空的结果。

应该使用Regex_search,而不是regex_match。程序现在输出消息的正确部分。