C++ 匹配由符号分隔的重复单词并忽略空格

C++ 匹配由符号分隔的重复单词并忽略空格,c++,regex,C++,Regex,如何匹配由特定字符分隔的字符,比如“;”忽略赛前和赛后的空间,但保留赛后的空间 字1;字2;word31 word32 Parantese仅表示匹配项 到目前为止,我已经有\s*[a-zA-Z0-9\s]*[a-zA-Z0-9]+\s*[;],但我不知道如何让这些单词重复。它还应该能够处理空单词,比如单词;;字,字;字或字;单词因为它忽略了空格,所以前两个应该是等价的 主要的问题是,我不知道如何处理拆分以及两个选项:合法单词和空单词,因为我的语句至少需要1个符号 或者,如果我允许重复使用分隔符,

如何匹配由特定字符分隔的字符,比如“;”忽略赛前和赛后的空间,但保留赛后的空间

字1;字2;word31 word32 Parantese仅表示匹配项

到目前为止,我已经有\s*[a-zA-Z0-9\s]*[a-zA-Z0-9]+\s*[;],但我不知道如何让这些单词重复。它还应该能够处理空单词,比如单词;;字,字;字或字;单词因为它忽略了空格,所以前两个应该是等价的

主要的问题是,我不知道如何处理拆分以及两个选项:合法单词和空单词,因为我的语句至少需要1个符号

或者,如果我允许重复使用分隔符,分隔符之间有空格,那么问题就可以解决了,但这又回到了我不知道如何处理分割的事实

编辑:我也打算在C++中使用它 编辑:可能就是这个,我能得到事实证明吗\s*[a-zA-Z0-9\s]*[a-zA-Z0-9]+[;]*\s*[;]*

试试这个:

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

int main()
{
    std::string s = "  w1 w2 w3;   word1 word2    ; word1  ;  ";

    for (std::smatch m; std::regex_search(s, m, std::regex("\\b([a-z0-9\\s]+)\\b", std::regex::icase)); s = m.suffix())
    {
        std::cout << m[1] << std::endl;
    }

    return 0;
}

由于带有嵌套量词的长regexp(即使是按照展开循环原则编写的)经常会导致std::regex出现问题,因此在这种情况下,拆分方法似乎是最好的

以下是一份:

模式在R\s*中定义\s*-它匹配用0+空格括起来的分号


注意:此方法可能需要从空白中修剪输入字符串,有关剥离前导/尾随空白的各种方法,请参阅。

仅使用此正则表达式拆分\s*\S*@ ANUBHAVA -考虑“Word1”;word2',在word2后面有尾随空格。你是说应用正则表达式两次?分为子字符串,然后将上一个应用于结果匹配?@Rob可能是[a-z0-9]+[;]*?Nvm我想我可能已经找到了,对吗?
w1 w2 w3
word1 word2
word1
#include <string>
#include <iostream>
#include <regex>
using namespace std;

int main() {
    std::vector<std::string> strings;
    std::string s = "word1; word2  ; word31 word32";
    std::regex re(R"(\s*;\s*)");
    std::regex_token_iterator<std::string::iterator> it(s.begin(), s.end(), re, -1);
    decltype(it) end{};
    while (it != end){
        strings.push_back(*it++);
    }
    for (auto& s: strings){ //std::cout << strings[strings.size()-1] << std::endl;
        std::cout << "'" << s << "'" << std::endl;
    }
    return 0;
}
'word1'
'word2'
'word31 word32'