Boost::Split using whole string as delimiter
Asked Answered
A

3

12

I would like to know if there is a method using boost::split to split a string using whole strings as a delimiter. For example:

str = "xxaxxxxabcxxxxbxxxcxxx"

is there a method to split this string using "abc" as a a delimiter? Therefore returning:

Results would be the string "xxaxxxx" and "xxxxbxxxcxxx".

I am aware of boost::split using the "is_any_of" predicate, however invoking is_any_of("abc") would result in splitting the string at the single character 'a', 'b', and 'c' as well, which is not what I want.

Abound answered 15/9, 2011 at 20:17 Comment(2)
You should be able to use some combination of equals and the string value to be used as delimiter, but the specifics elude me right now.Hinze
possible duplicate of Split on substringFrasquito
B
6

split_regex as suggested by @Mythli is fine. If you don't want to deal with regex, you can use ifind_all algo, as is shown in this example. You receive iterator_range (begin/end) of all occurrences of you delimiter. Your tokens are between them (and at the beginning and end of string).

Boulder answered 15/9, 2011 at 20:43 Comment(1)
Thanks, this solution fit my needs perfectly.Abound
E
9

Yes there is a way (this is a way I know, maybe there is a better way) Use boost::algorithm::split_regex to split character sequences where delimiters are regular expressions.

Example:

vector< string > result;
boost::algorithm::split_regex( result, str, regex( "^((?!abc)*abc(?!abc)*)*$" ) ) ;
copy( result.begin(), result.end(), ostream_iterator<string>( cout, "\n" ) ) ;
Evans answered 15/9, 2011 at 20:30 Comment(2)
Thanks, this solution works, however it requires boost to be built. Currently I am using header files only.Abound
#include <boost/algorithm/string/regex.hpp>Ascendant
B
6

split_regex as suggested by @Mythli is fine. If you don't want to deal with regex, you can use ifind_all algo, as is shown in this example. You receive iterator_range (begin/end) of all occurrences of you delimiter. Your tokens are between them (and at the beginning and end of string).

Boulder answered 15/9, 2011 at 20:43 Comment(1)
Thanks, this solution fit my needs perfectly.Abound
P
1

If you know what characters your input string is comprised of (e.g. a simple English sentence, with only EN characters), you can surround your word_delimiter with a special_symbol, replace all appearances of your word delimiter with it and split by the special_symbol.

For example, I used '%' here:

std::vector<std::string> sentence_parts;
boost::replace_all(sentence, word, "%" + word_delimiter + "%");
boost::split(sentence_parts, sentence, boost::is_any_of("%"));

You need to be sure that your input string will never have the special symbol in it, otherwise it won't work well!

Psychologize answered 27/12, 2020 at 9:31 Comment(2)
Why would this be preferable to the more straightforward approach Mythli already described?Leschen
It's not necessarily preferable, it's another way to do it. Personally, I prefer not to deal with regexes...Psychologize

© 2022 - 2024 — McMap. All rights reserved.