不需要的正则表达式捕获

Unwanted regex capturing

本文关键字:正则表达式 不需要      更新时间:2023-10-16

以下正则表达式应该匹配夹在两个非字母数字字符之间的 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 字符,您需要考虑对第一组使用"正后看断言",对最后一组使用"正前瞻断言"。 这些断言,即使它们看起来有点像其他组,实际上并不包括它们在结果中匹配的内容。