正则表达式:如何找到一个模式的最大整数值? [英] Regex: how to find the maximum integer value of a pattern?

查看:156
本文介绍了正则表达式:如何找到一个模式的最大整数值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一下,我有以下字符串:

 我有些%1%,有的%42%,也许有些%5%为好。

基本上,我感兴趣的是知道遵循的模式%(整数)%的最大整数值。

我什至不知道这是可能的正则表达式做。正则表达式是什么,我可以使用,这样在上面的例子中,答案是42?

P.S。一个简单的解决方案显然是简单地寻找任何%(整数)%图案,并使用脚本(C ++ code)通过所有比赛进行迭代,找到的最高值。我的问题是:是否有可能做到这一点正则表达式中的直线距离

背景:了解接下来恐怕没有必要回答这个问题,但我想一些你可能想知道

基本上我使用C ++和boost ::格式。格式是用图案占位符如下:%1%%2%等升压::格式抛出一个异常,如果提供的变量的数量不符合格式本身的最大整数值。我将要使用的格式由(信任)用户(网站管理员)提供。不过,做正确的事情,我需要验证彭定康。因此,我需要找到在模式的最大整数,以确保没有异常会在运行时被抛出。

如果您正在使用boost ::格式的用户提供的格式,你是怎么处理这个问题?

顺便说一下,没有升压格式标签! (虽然也有其他的升压富标记)。

解决方案

<一个href=\"http://stackoverflow.com/questions/4170440/regex-how-to-find-the-maximum-integer-value-of-a-pattern/4170472#4170472\">Billy奥尼尔提供正确的答案和白塞头张彻(在评论到所选答案)还跟粘贴实际code:

 的#include&LT;&iostream的GT;
#包括LT&;串GT;
#包括LT&;&双端GT;
#包括strtk.hpp诠释的main()
{
   性病::字符串s =我有些%1%,有的%42%,也许有些%5%为好。
   的std :: deque的&LT; INT&GT; int_list;
   strtk :: split_regex(%([\\\\ D]。+)%,
                       S,
                       strtk :: range_to_type_back_inserter(int_list)
                       strtk :: match_mode :: match_1);   如果(!int_list.empty())
   {
        性病::法院LT&;&LT; 最大:&LT;&LT; strtk :: max_of_cont(int_list)LT;&LT;的std :: ENDL;
   }   返回0;
}


解决方案

找到这样所有的值:%([\\ D] +)%,解析反向引用作为一个整数(使用类似的lexical_cast ),并选择最高值。 (您可以使用类似的std :: max_element 本)

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;
}

解决方案

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)

这篇关于正则表达式:如何找到一个模式的最大整数值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆