已检查匹配项时接收到C++正则表达式空字符串
C++ regex empty strings received on checked matches
我正在编写一个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
。程序现在输出消息的正确部分。
相关文章:
- 使用正则表达式regex_search在字符串中查找字符串
- 在 C++ 中使用正则表达式错误时出现问题 括号表达式中的范围无效
- C++正则表达式无限循环
- FindPackageHandleStandardArgs.cmake:137 的 CMake 错误(消息):找不到 Boost (缺少:正则表达式)(找到合适的版本"1.72.0",
- 定义有趣的宏和正则表达式在Z3 C++绑定
- 带有多个字符分隔符的正则表达式
- C++ 使用增强正则表达式库时断言崩溃
- 有人可以帮助我处理正则表达式吗?
- 使用正则表达式获取大括号块的列表
- 正则表达式以匹配数字的重复模式,后跟任何类型的分隔符?
- 组合正则表达式部分使用 | 不起作用的 C++
- 为什么C++正则表达式这么慢?
- 如何使HTML5电子邮件验证正则表达式在C++中工作?
- 在C++中实现正则表达式
- C++正则表达式替换整个单词
- 用C++编写正则表达式的正确方法是什么?
- 如何从Stroustrup的C++书中解释这个正则表达式?
- 为什么这个正则表达式C++在括号表达式中抛出无效范围异常?
- C++:匹配正则表达式,什么是匹配?
- C++:正则表达式匹配代码,打印多个匹配项?