Check if the string contains a series of numbers

I would like to test a std::string in order to contain numbers of any range, for example 5 to 35 in std::string s = "XDGHYH20YFYFFY" , would there be a function or would I have to convert the number to a string, and then use a loop, to find each one?

+4
source share
4 answers

I would probably use a locale that treats everything except numbers as white space, and read numbers from a string stream saturated with this locale, and check if they are in the range:

 #include <iostream> #include <algorithm> #include <locale> #include <vector> #include <sstream> struct digits_only: std::ctype<char> { digits_only(): std::ctype<char>(get_table()) {} static std::ctype_base::mask const* get_table() { static std::vector<std::ctype_base::mask> rc(std::ctype<char>::table_size,std::ctype_base::space); std::fill(&rc['0'], &rc['9'], std::ctype_base::digit); return &rc[0]; } }; 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 < lower || upper < n) return false; return true; } int main() { std::cout << std::boolalpha << in_range(5, 35, "XDGHYH20YFYFFY"); return 0; } 
+9
source

While some people are about to go straight into regex, I think your best bet is a hybrid solution. Use REGEX to search for numbers, then analyze them and see if they are in a range.

Something like this in C #. Not sure which regular expression 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 <= max) return true; } return false; } 
+2
source

If you are looking for all the numbers in a range, use the string :: find function in a loop. Here is the link for the function:

http://www.cplusplus.com/reference/string/string/find/

Also do you want to use numbers in a string only once? For example, SDFSD256fdsfs will give you the numbers 2 5 6 25 56 256, unless you remove them from the line after each match.

0
source

You can do it iteratively with a stringstream object.

 #include <iostream> #include <string> #include <sstream> #include <cctype> void check (std::string& s) { std::stringstream ss(s); std::cout << "Searching string: " << s << std::endl; while (ss) { while (ss && !isdigit(ss.peek ())) ss.get (); if (! ss) break; int i = 0; ss >> i; std::cout << " Extraced value: " << i << std::endl; } } int main () { std::string s1 = "XDGHYH20YFYFFY"; std::string s2 = "20YF35YFFY100"; check (s1); check (s2); return 0; } 

Productivity:

 Searching string: XDGHYH20YFYFFY Extraced value: 20 Searching string: 20YF35YFFY100 Extraced value: 20 Extraced value: 35 Extraced value: 100 

Adding parameters to the check function to limit the accepted results will be trivial.

0
source

Source: https://habr.com/ru/post/1313551/


All Articles