不需要的正则表达式捕获
Unwanted regex capturing
以下正则表达式应该匹配夹在两个非字母数字字符之间的 YYYY-MM-DD 形式的日期。 它应该只提取日期而不是两个非字母数字字符......但它的作用恰恰相反。 我做错了什么。PS 我已经尝试将 [^:alnum:] 包围在非捕获组 (?:) 中,但它不起作用。
regex exp1("[^:alnum:]([1-9][0-9]{3}(?:-[0-9][1-9]){2})[^:alnum:]")
//or
regex exp1("[^a-zA-Z0-9]([1-9][0-9]{3}(?:-[0-9][1-9]){2})[^a-zA-Z0-9]")
您也可以访问此网站试用我的正则表达式,而无需为其编写C +代码。复制并粘贴非POSIX括号表达式(不带引号),如果您选择使用该网站:
正则表达式在线测试仪
#include <regex>
#include <string>
#include <iostream>
#include <vector>
#define isthirty(x) for (int i = 0; i < 3; i++) {if (days[i] == x[1]) {thirty = true;break;}}
using namespace std;
int main() {
vector<string> words;
string str;
getline(cin, str);
int N = stoi(str);
int days[] = { 4,6,9,11 };
regex exp1("[^a-zA-Z0-9]([1-9][0-9]{3}(?:-[0-9][1-9]){2})[^a-zA-Z0-9]");
for (int i = 0; i < N; i++) {
getline(cin, str);
sregex_iterator it(str.cbegin(), str.cend(), exp1);
sregex_iterator end;
for (; it != end; it++) {
words.push_back(it->str(0));
}
}
regex exp2("([0-9])+");
for (auto &it : words) {
int dates[3] = {};
sregex_iterator pos(it.cbegin(), it.cend(), exp2);
sregex_iterator end;
str = it.substr(1,10);
for (int i = 0; pos != end; pos++, i++) {
dates[i] = stoi(pos->str(0));
}
if (dates[0] > 2016 || dates[1] > 12 || dates[2] > 31) {
continue;
}
bool thirty = false;
isthirty(dates);
if (thirty && dates[2] <= 30) {
cout << str << "n";
}
else if(dates[1] == 2) {
if (dates[0] % 4 == 0 && dates[2] <= 29) {
cout << str << "n";
}
else if (dates[0] % 4 != 0 && dates[2] <= 28) {
cout << str << "n";
}
}
else if (dates[2] <= 31) {
cout << str << "n";
}
}
return 0;
}
尝试简化正则表达式:
[^0-9]([0-9]{4}-[0-9]{2}-[0-9]{2})[^0-9]
它查找非数字,然后查找 YYYY-MM-DD 日期,然后查找非数字。它捕获日期。适用于几乎所有正则表达式口味。
在您提供的正则表达式中,整个正则表达式(也称为组 0)将包含两个非字母数字字符,但捕获组 1 应仅包含您感兴趣的日期。 因此,您可以按原样使用正则表达式,然后从组 1 中提取信息。
如果您实际上想更改正则表达式以不包含非 alphanum 字符,您需要考虑对第一组使用"正后看断言",对最后一组使用"正前瞻断言"。 这些断言,即使它们看起来有点像其他组,实际上并不包括它们在结果中匹配的内容。
相关文章:
- FindPackageHandleStandardArgs.cmake:137 的 CMake 错误(消息):找不到 Boost (缺少:正则表达式)(找到合适的版本"1.72.0",
- 组合正则表达式部分使用 | 不起作用的 C++
- C++ 正则表达式不匹配空格
- 调试正则表达式不匹配
- 为什么regex_match与我的正则表达式不匹配
- C++:矢量<string>与正则表达式不匹配
- 提升正则表达式不匹配
- 正则表达式不允许在 dot(.) 之后立即写入 dot(.)
- 我的 XML 标签正则表达式不起作用
- C++11正则表达式不匹配任何内容
- 不需要的正则表达式捕获
- 简单正则表达式不匹配
- Boost正则表达式不替换正则表达式网站上正确替换的表达式
- 我的最后一个正则表达式不起作用,但我无法弄清楚原因
- Qt正则表达式不匹配
- 无法弄清楚为什么正则表达式不起作用
- POSIX正则表达式不匹配
- 正则表达式不能以零开头
- 为什么这个正则表达式不能编译
- C++ 增强正则表达式不保存捕获