jmasterx
jmasterx

Reputation: 54103

Parse string where "separator" can be part of data?

I have std strings like these:

UserName: Message

At first look it seems like an easy problem, but this issue is in that the name's last character could be a ':' and the first letter of the message part of the string could be a ':' too. The user could also have spaces in their name.

So A user might be names 'some name: ' and might type a message ' : Hello'

Which would look like: 'some name: : : Hello'

I do have the list (vector) of usernames though.

Given this, is there a way I could extract the username from this sort of string? (Ideally without having to iterate through the list of users)

Thanks

Upvotes: 1

Views: 242

Answers (4)

Jonathan Wood
Jonathan Wood

Reputation: 67195

The way I would approach this is to simply find the first colon. Split the string there, and then trim the two remaining strings.

It's not entirely clear to me why there are additional colons and if they are part of the value. If they need to be removed, then you'll also need to strip them out.

Upvotes: 0

Erwald
Erwald

Reputation: 2206

I would use string tokens

string text = "token, test   string";

char_separator<char> sep(":");
tokenizer< char_separator<char> > tokens(text, sep);
BOOST_FOREACH(string t, tokens)
{
    cout << t << "." << endl;
}

Upvotes: 0

Derek Risling
Derek Risling

Reputation: 364

If you can't gaurentee that the username won't contain a ":" characters, and you want to avoid iterating the entire list each time to check, you could try a shortcut.

Keep a vector of just the usernames that contain special chars (I'm imagining that this is a small subset of all usernames). Check those first, if you find a match, take the string after [username]: . Otherwise, you can simply do a naive split on the colon.

Upvotes: 1

nes1983
nes1983

Reputation: 15756

Try a regex like (\w+?):\ \w+.

Upvotes: 1

Related Questions