1

I took this code for string split here: string split

char sep = ' ';
std::string s="1 This is an exampl";

for(size_t p=0, q=0; p!=s.npos; p=q)
std::cout << s.substr(p+(p!=0), (q=s.find(sep, p+1))-p-(p!=0)) << std::endl;

The code works ok and the output is:

1
This
is
an
exampl

If I change the separator from ' ' to 'e', the output is:

1 This is an
xampl

The 'e' in exampl is lost. How can I split the string using the same code but not losing the letter used as the separator?

Community
  • 1
  • 1
Massimo D. N.
  • 316
  • 1
  • 6
  • 17
  • 4
    Which part would you expect the separator to be in? Including the separator in the split output makes little sense, that's a rather rare goal. – unwind Nov 03 '16 at 09:25
  • 3
    You know that each part was separated by an `'e'`, so you can just print an extra `'e'` after each piece except the last. But it sounds like you are parsing something in a very clumsy way. You should probably search for how to parse properly. – nwp Nov 03 '16 at 09:26
  • I get the impression you need to split on an empty position before an `e` that is starting a word, right? – Wiktor Stribiżew Nov 03 '16 at 09:29

1 Answers1

1

I suggest splitting the string with a simple \b(?=e) regex (matching an e only if it is not preceded with a letter, digit or underscore):

#include <string>
#include <iostream>
#include <regex>
using namespace std;

int main() {
    std::vector<std::string> strings;
    std::string s = "1 This is an exampl";
    std::regex re("\\b(?=e)");
    std::regex_token_iterator<std::string::iterator> it(s.begin(), s.end(), re, -1);
    decltype(it) end{};
    while (it != end){
        strings.push_back(*it++);
        std::cout << strings[strings.size()-1] << std::endl; // DEMO!
    }
    return 0;
}

See the C++ demo.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Thank you its ok. And how can i store '1 This is an' in var1 and 'example' in var2? – Massimo D. N. Nov 04 '16 at 10:28
  • 1
    Well, if you have `exampl` in the input. If you have `1 This is an example` as input, you may use `re("\\s+(?=e)")` regex (or, better with a raw string literal - `re(R"(\s+(?=e))")`) where `\s+` matches 1 or more whitespaces. See [this demo](https://ideone.com/9M3ysL). – Wiktor Stribiżew Nov 04 '16 at 10:34
  • i tryed your new code but have a strange porblem: if i run the code online (c++ shell) it works. If run it under CodeBlock it print out only example. Anyway, i want to put the two parts in two different variables(say var1 and var2). How can i modify the last line to have it? Thanks – Massimo D. N. Nov 04 '16 at 11:21
  • Ideone stopped working. I can suggest getting the first two values if present from the `strings` vector`. – Wiktor Stribiżew Nov 04 '16 at 12:22