user360907
user360907

Reputation:

Parse std::string for a selection of characters

Is there an easy way to parse a std::string in search of a list of certain charcters? For example, let's say the user enters this<\is a.>te!st string. I'd like to be able to spot those non-letter characters are there and do something about it. I'm looking for a general purpose solution that allows me to simply specify a list of chars so I can reuse the function in different situations. I'm guessing regular expressions will play a key role in any solution, and obviously the more compact and effience, the better.

Upvotes: 2

Views: 231

Answers (3)

Archie
Archie

Reputation: 6854

Regex seems like overkill. You can use std::string's methods: find_first_of() and/or find_last_of(). Here you can find documentation and examples.

Upvotes: 0

mkaes
mkaes

Reputation: 14129

How about using a regex library like boost::regex?
This should exactly do what you are looking for.
If your compiler supports C++11 you can use std::regex.

Upvotes: 2

NPE
NPE

Reputation: 500913

You could use std::string::find_first_not_of() for this. It'll find the characters except those in the set that you give it. Its counterpart, find_first_of(), will search for characters that are in the set.

Both functions allow you to specify the starting index. This will enable you you to continue the search from where you left off.

Upvotes: 5

Related Questions