Regex: how to find the maximum integer value of a

2019-03-11 05:41发布

Imagine I have the following string:

"I'll have some %1%, some %42% and maybe some %5% as well."

Basically, I am interested in knowing the maximum integer value that follow the pattern %(integer)%.

I am not even sure it's possible to do with a regex. What regex could I use so that in the above example the answer would be 42?

P.S. One easy solution is evidently to simply look for any %(integer)% patterns, and use the script (c++ code) to iterate through all the matches and find the highest value. My question is: is it possible to do it straight away within the regex?

Background: understanding what follows is probably not necessary to answer the question, but I thought some of you might want to know.

Basically I am using C++ and boost::format. Formats are patterned with placeholders like this: %1%, %2%, etc. Boost::format throws an exception if the number of supplied variables do not correspond to the maximum integer value in the format itself. The formats I am going to use are supplied by (trusted) users (web site administrators). Still, to do things properly, I need to validate the patten. Thus, I need to find the maximum integer in the pattern to make sure no exception will be thrown at run time.

If you are using boost::format with user-supplied formats, how did you deal with this issue?

BTW, there is no boost-format tag! (although there are other boost-foo tags).

Solution

Billy ONeal provided the right answer and Beh Tou Cheh (in the comments to the selected answer) was kind enough to paste the actual code:

#include <iostream>
#include <string>
#include <deque>
#include "strtk.hpp"

int main() 
{
   std::string s = "I'll have some %1%, some %42% and maybe some %5% as well.";
   std::deque<int> int_list;
   strtk::split_regex("%([\\d]+)%",
                       s,
                       strtk::range_to_type_back_inserter(int_list),
                       strtk::match_mode::match_1);

   if (!int_list.empty())
   {
        std::cout << "max: " << strtk::max_of_cont(int_list) << std::endl;
   }

   return 0;
}

2条回答
可以哭但决不认输i
2楼-- · 2019-03-11 05:53

Find all values like this: %([\d]+)%, parse the back reference as an integer (using something like lexical_cast), and choose the highest value. (You can use something like std::max_element for this)

查看更多
聊天终结者
3楼-- · 2019-03-11 06:02

Even if someone did devise a regex capable of doing that, it would not perform as well as simply iterating the matches.

查看更多
登录 后发表回答