regex C++查找字符串中正则表达式匹配的位置

8mmmxcuj  于 2023-04-22  发布在  其他
关注(0)|答案(2)|浏览(159)

我的算法有一些问题,试图获取字符串中正则表达式匹配的位置。请在这里找到代码-https://godbolt.org/z/aj8Gq4Moh

std::vector<int> mval;

int closed_val = 0;
int open_val = 0;
std::string da_line = "} } }";
std::smatch m;
std::regex_search(da_line, m, std::regex("([\\}]+)"));
for (auto mv : m) std::cout << mv << std::endl;
std::cout << m.size() << std::endl;
closed_val = da_line.find(m[0]);
mval.push_back(closed_val);
try {
    if (m.size() >= 1) {
        std::string db_line;
        for (int j = 1; j < m.size(); j++) {
            std::cout << da_line << std::endl;
            db_line = da_line.substr(da_line.find(m[j]) + 1, da_line.size() - 1);
            std::cout << db_line << std::endl;
            int f = db_line.find(m[j]);
            if (f == -1 || f > db_line.size() - 1)
                continue;
                std::cout << f << std::endl;
            open_val = closed_val + f + 1;
            for (auto mv : m) std::cout << mv << std::endl;
            std::cout << db_line.find(m[j + 1]) << std::endl;
            mval.push_back(open_val);
            closed_val = open_val;
            da_line = db_line;
        }
    }
}
catch (std::exception& error) {
    std::cerr << "error : " << error.what() << std::endl;
}

for (auto mv : mval) std::cout << mv << ":";
std::cout << std::endl;

输出:

}
}
2
} } }
 } }
1
}
}
0
0:2:

1.为什么正则表达式返回2个匹配项,而实际上有3个匹配项,
1.当它返回0:2时,预期结果是0:2:4

jtoj6r0c

jtoj6r0c1#

你对函数std::regex_search有一个基本的误解。它只会搜索1匹配(或者什么都不搜索)。请再读一遍here
另外,std::match_results的工作方式确实完全不同。但是在std::regex_search的描述中有一个非常好的解释。
请注意:
如果可以找到正则表达式的匹配项,基本上对于'}',m.size()将是2,并且只有m[0]是相关的。
std::match_results将只包含一个以上的结果,如果有一个以上的子匹配。|B”你可以有两个元素。
您需要遵循功能描述中的“注解”部分。
为了检查目标序列中的所有匹配,可以在循环中调用std::regex_search,每次从前一次调用的m[0].second开始重新启动。std::regex_iterator为这次迭代提供了一个简单的接口。
而且,基本上,对于您的特定愿望,要找到'}'在字符串中的位置,您根本不需要std::regex_search。循环中的.find()就完全足够了。
但我理解您希望使用更通用的“regex”方法。
目前我没有看到一个好的解决方案。然而,以下将为您的示例工作:

#include <iostream>
#include <string>
#include <regex>

int main() {
    // We want to search in this string
    std::string line{ " abx    aby   abz" };

    // regex for search a }
    const std::regex re{ R"(ab?)" };

    // Here we will store the running position
    std::size_t position{};

    // Search all occurences of regex in the given string
    for (std::smatch sm; std::regex_search(line, sm, re); line = sm.suffix()) {

        // Search the character and add to current position
        position += line.find(sm[0]);

        // Show result
        std::cout << position << ' ';

        // Take length of regex into account
        position += std::string(sm[0]).size();
    }
}
myzjeezk

myzjeezk2#

要找到输入中的所有匹配项,通常需要使用regex_iterator(在本例中,具体为sregex_iterator),顺序如下:

#include <regex>
#include <iostream>
#include <string>

int main() {
    std::string s = "} } }";

    std::regex pattern("([\\}]+)");
    auto begin = std::sregex_iterator {s.begin(), s.end(), pattern};
    auto end = std::sregex_iterator();

    for (std::sregex_iterator i = begin; i != end; ++i)
    {
        std::cout << "Found match at position: " << i->position() << "\n";
    }
}

结果:

Found match at position: 0
Found match at position: 2
Found match at position: 4

相关问题