我的正则表达式遇到麻烦。
regex nameInt("([A-Za-z]+)\\: ([0-9]+)");
regex nameDecimal("([a-zA-Z]+)\\: ([-+]?[0-9]+\\.[0-9]+)");
我的输入如下:
Jenny: 12
Mark: 12.6
但是我的输入仅标记小数,而不标记整数。
尝试这些表达式
具有int的行:
[a-zA-Z]+\: \d+$
带小数的行:
[a-zA-Z]+\: \d+\.\d+$
这里是代码:
#include <iostream>
#include <regex>
#include <vector>
int main() {
using namespace std;
vector<string> v_s = {"Jenny: 12", "Mark: 12.6"};
regex reg_int("^(.\\D+)\\: ([+-]?.\\d+)$");
regex reg_double("^(.\\D+)\\: ([+-]?.\\d+\\.\\d+)$");
for (const auto &s : v_s) {
if (std::regex_match(s, reg_int))
cout << "line with int value : " << s << endl;
if (std::regex_match(s, reg_double))
cout << "line with double value : " << s << endl;
}
return 0;
}
输出:
line with int value : Jenny: 12
line with double value : Mark: 12.6
使用您的正则表达式也可以。您能否提供更多详细信息?