Check if string contains a range of numbers?

I'd probably use a locale that treated everything except digits as white-space, and read the numbers from a stringstream imbued with that locale and check if they're in range.

I'd probably use a locale that treated everything except digits as white-space, and read the numbers from a stringstream imbued with that locale and check if they're in range: #include #include #include #include #include struct digits_only: std::ctype { digits_only(): std::ctype(get_table()) {} static std::ctype_base::mask const* get_table() { static std::vector rc(std::ctype::table_size,std::ctype_base::space); std::fill(&rc'0', &rc'9', std::ctype_base::digit); return &rc0; } }; bool in_range(int lower, int upper, std::string const &input) { std::istringstream buffer(input); buffer. Imbue(std::locale(std::locale(), new digits_only())); int n; while (buffer>>n) if (n.

As much as some people are going to immediately go to regex on this, I think your best bet is actually a hybrid solution. Use REGEX to find numbers, then parse them and see if they're in range. Something like this in C#.

Not sure what regex libraries are available to you in C++. Using System.Text. RegularExpressions.

Regex; using System.Text. RegularExpressions. Match; using System.Text.

RegularExpressions. MatchCollection; private static const Regex NUMBER_REGEX = new Regex(@"\d+") public static bool ContainsNumberInRange(string str, int min, int max) { MatchCollection matches = NUMBER_REGEX. Matches(str); foreach(Match match in matches) { int value = Convert.

ToInt32(match. Value); if (value >= min && value.

You could use boost::regex in C++. I'm not sure about the \d+ expression; you can be more precise. For instance, the range 3-35 would be found more efficiently with \d{1,2} (assuming that X200Y matches) – MSalters Jun 23 '10 at 9:19 I assumed that X200Y should not match, because that should parse as 200, which is not in range.

The problem wasn't specified well enough to know this for sure, though. – jdmichal Jun 23 '10 at 18:07.

If you are searching for all of the numbers in the range then use string::find function in a loop. Here is the reference for the function: cplusplus.com/reference/string/string/find/ Also do you want to use the numbers in the string only once? For example SDFSD256fdsfs will give you the numbers 2 5 6 25 56 256 if you don't remove them from the string after each match.

You can do this iteratively with a stringstream object. #include #include #include #include void check (std::string& s) { std::stringstream ss(s); std::cout > i; std::cout.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions