Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 495459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:35:43+00:00 2026-05-13T05:35:43+00:00

How can I split a string with Boost with a regex AND have the

  • 0

How can I split a string with Boost with a regex AND have the delimiter included in the result list?

for example, if I have the string “1d2” and my regex is “[a-z]” I want the results in a vector with (1, d, 2)

I have:

std::string expression = "1d2";
boost::regex re("[a-z]");
boost::sregex_token_iterator i (expression.begin (),
                                expression.end (), 
                                re);
boost::sregex_token_iterator j;
std::vector <std::string> splitResults;
std::copy (i, j, std::back_inserter (splitResults)); 

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T05:35:43+00:00Added an answer on May 13, 2026 at 5:35 am

    I think you cannot directly extract the delimiters using boost::regex. You can, however, extract the position where the regex is found in your string:

    std::string expression = "1a234bc";
    boost::regex re("[a-z]");
    boost::sregex_iterator i(
      expression.begin (),     
      expression.end (),     
      re);
    boost::sregex_iterator j;
    for(; i!=j; ++i) {
      std::cout << (*i).position() << " : " << (*i) <<  std::endl;
    }
    

    This example would show:

    1 : a

    5 : b

    6 : c

    Using this information, you can extract the delimitiers from your original string:

    std::string expression = "1a234bc43";
    boost::regex re("[a-z]");
    boost::sregex_iterator i(
      expression.begin (),     
      expression.end (),     
      re);
    boost::sregex_iterator j;
    size_t pos=0;
    for(; i!=j;++i) {
      std::string pre_delimiter = expression.substr(pos, (*i).position()-pos); 
      std::cout << pre_delimiter << std::endl;
      std::cout << (*i) << std::endl;
      pos = (*i).position() + (*i).size();
    }
    std::string last_delimiter = expression.substr(pos);
    std::cout << last_delimiter << std::endl;
    

    This example would show:

    1

    a

    234

    b

    c

    43

    There is an empty string betwen b and c because there is no delimiter.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I split string for 3 character? (I don't want to do loop
How can i split data from string? I have my string like this, Url=http://www.yahoo.com
How can I split a string using [ as the delimiter? String line =
I have a string like below string str = ABE,1A2B3CD,1B2A3C,1A2BC3E; How can i split
I have a problem about SQL. How can i split a string with a
How can I split a string by a delimiter, but not if it is
I have this string 8S8Q4D1SKCQC2C4S6H4C6DJS2S1C6C How can I split this string into substrings that
I have a string of format [id1,id2,id3] . i'm using boost split to split
I have a string that looks like this: abc[1,2,3].something.here,foo[10,6,34].somethingelse.here,def[1,2].another I want to split this
I want to know how i can split a string in php based on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.