S
S
Sergo Zar2020-12-04 20:40:47
C++ / C#
Sergo Zar, 2020-12-04 20:40:47

How to split text into words?

Please help with regular expressions.
I'm here https://riptutorial.com/ru/cplusplus/topic/1681/re... I'm reading about regular expressions and I can't figure out how to split the text into words.

here I tried to replace from this example https://ideone.com/nSRXEa
"(.*)\".*\\breg.*\\bex.*\"\\s*$"
with
"\\W+", as I did in python before (re.split(r'\W+', 'example text') and as a result I got ["example","text" ]), but nothing happens. I love something I don’t catch up with but what?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yuriy Vorobyov, 2020-12-13
@YuriyVorobyov1333

You can read more about regex_token_iterator here and here .

#include <iostream>
#include <regex>
#include <string>
 
using namespace std;
 
int main() {
  const auto input = "Some people, when confronted with a problem, think \"I know, I'll use regular expressions.\""s;
  
  regex rgx("\\W+");

  sregex_token_iterator iter(input.begin(),
    input.end(),
    rgx,
    -1);
  
  sregex_token_iterator end;

  while (iter != end) {
    std::cout << *iter << endl;
    ++iter;
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question