0

I dont know if it makes any sense or not but here it is
Is there a way that i can get two words from a regex result each time?
supose i have a text file which contains an string such as the following :

Alex Fenix is an Engineer who works for Ford Automotive Company. His Personal ID is <123456>;etc....

basically if i use \w i would get a list of :

Alex
Fenix
is 
an
Engineer
and etc 

They are all separated by white space and punctuation marks what i am asking is , whether there is a way to have a list such as :

Alex Fenix
is an
Engineer who
works for
Ford Automotive
Company His
Personal ID
is 123456

How can i achieve such a format?
Is it even possible or should i store those first results in an array and then iterate through them and create the second list?
By the way please note that the item Alex Fenix is actually an abstraction of a map or any container like that.
The reason i am asking is that i am trying to see if there is any way that i can directly read a file and apply a regex on it and get this second list without any further processing overhead (I mean reading into a map or string , then iterating through them and creating pairs of the tokens and then carry on what ever is needed )

4

1 回答 1

3

试试这个正则表达式

\w \w

它将匹配任何后跟空格和另一个单词的单词。

尽管您可以在不使用正则表达式的情况下相对容易地实现这种格式。看一下这个例子:

#include <iostream>
#include <sstream>
#include <string>
#include <algorithm>

int main() {
    std::string s("Alex Fenix is an Engineer who works for Ford Automotive Company. His Personal ID is <123456>");

    // Remove any occurences of '.', '<' or '>'.
    s.assign(begin(s), std::remove_if(begin(s), end(s), [] (const char c) {
        return (c == '.' || c == '<' || c == '>');
    }));

    // Tokenize.
    std::istringstream iss(s);
    std::string t1, t2;
    while (iss >> t1 >> t2) {
        std::cout << t1 << " " << t2 << std::endl;
    }
}

输出:

Alex Fenix
is an
Engineer who
works for
Ford Automotive
Company His
Personal ID
is 123456
于 2013-07-21T14:14:37.457 回答