标签: boost-regex

提升正则表达式和混淆错误

我正在尝试使用Boost正则表达式来查看其中是否有某个整数.

此页面上的一个示例是

bool validate_card_format(const std::string& s)
{
   static const boost::regex e("(\\d{4}[- ]){3}\\d{4}");
   return regex_match(s, e);
}
Run Code Online (Sandbox Code Playgroud)

还有一个可能是工作示例这里.

但是当我在我的机器上尝试它时,我得到五页不可读的错误.这是怎么回事?

#include <boost/regex.hpp>
#include <iostream>

using namespace std;

int main(int argc, char *argv[]) {
    static const boost::regex rxInt("[0-9]+");
    string word = "foobar";

    if(boost::regex_match(word, rxInt)) {
        cout << "Integer" << endl;
    } else {
        cout << "Not integer" << endl;
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

$ g ++ test.cpp的错误.是否有一些理智的方式来解决这里发生的事情?

/tmp/ccRNnDit.o: In function `bool boost::regex_match<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, …
Run Code Online (Sandbox Code Playgroud)

c++ boost boost-regex

5
推荐指数
1
解决办法
9756
查看次数

如何使用Boost :: regex_search捕获重复组的所有匹配项?

我试图使用正则表达式解析输入字符串.在尝试捕获重复组时遇到问题.我似乎总是匹配该组的最后一个实例.我尝试过使用Reluctant(非贪婪)量词,但我似乎错过了一些东西.有人可以帮忙吗?

正则表达尝试:

(OS)\\s((\\w{3})(([A-Za-z0-9]{2})|(\\w{3})(\\w{3}))\\/{0,1}){1,5}?\\r

(OS)\\s((\\w{3}?)(([A-Za-z0-9]{2}?)|(\\w{3}?)(\\w{3}?))\\/{0,1}?){1,5}?\\r
Run Code Online (Sandbox Code Playgroud)

输入字符串:

OS BENKL/LHRBA/MANQFL\r\n
Run Code Online (Sandbox Code Playgroud)

我似乎总是得到最后一组是MANQFL组(MAN QFL),我的目标是获得所有三组(可以有1-5组):

(BEN KL) , (LHR BA) and (MAN QFL). 
Run Code Online (Sandbox Code Playgroud)

C++代码段:

std::string::const_iterator start = str.begin(), end = str.end(); 
while(regex_search(start,end,what,expr)) 
{ 
  cout << what[0]; 
  cout << what[1]; 
  ... 
  start += what.position () + what.length (); 
}
Run Code Online (Sandbox Code Playgroud)

这个循环只有一次,而我希望它在这个例子中运行3次.任何帮助都感激不尽.

c++ regex boost-regex

5
推荐指数
2
解决办法
3314
查看次数

为什么我的Boost.Regex搜索只报告一次匹配迭代?

我试图找出字符串中有多少正则表达式匹配.我正在使用迭代器迭代匹配,并使用整数来记录有多少.

long int before = GetTickCount();
string text;

boost::regex re("^(\\d{5})\\s(\\d{8})\\s(.*)\\s(.*)\\s(.*)\\s(\\d{8})\\s(.{1})$");
char * buffer;
long length;
long count;
ifstream f;


f.open("c:\\temp\\test.txt", ios::in | ios::ate);
length = f.tellg();
f.seekg(0, ios::beg);

buffer = new char[length];

f.read(buffer, length);
f.close();

text = buffer;
boost::sregex_token_iterator itr(text.begin(), text.end(), re, 0);
boost::sregex_token_iterator end;

count = 0;
for(; itr != end; ++itr)
{
    count++;
}

long int after = GetTickCount();
cout << "Found " << count << " matches in " << (after-before) << " ms." << endl;
Run Code Online (Sandbox Code Playgroud)

在我的例子中,count总是返回1,即使我把代码放在for循环中以显示匹配(并且有很多).这是为什么?我究竟做错了什么? …

c++ regex iterator greedy boost-regex

4
推荐指数
1
解决办法
3214
查看次数

boost示例无法构建

我是关于提升的新手.我成功编译了boost库(在mac os x下).现在,我尝试构建boost网站上提到的第一个示例(包括boost/as include目录和boost/stage/lib作为库目录,使用netbeans)并得到以下错误

"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
"/usr/bin/make"  -f nbproject/Makefile-Debug.mk dist/Debug/GNU-MacOSX/boost_ex1
mkdir -p build/Debug/GNU-MacOSX
rm -f build/Debug/GNU-MacOSX/main.o.d
g++    -c -g -I../../boost_1_44_0 -MMD -MP -MF build/Debug/GNU-MacOSX/main.o.d -o build/Debug/GNU-MacOSX/main.o main.cpp
mkdir -p dist/Debug/GNU-MacOSX
g++     -o dist/Debug/GNU-MacOSX/boost_ex1 build/Debug/GNU-MacOSX/main.o -L../../boost_1_44_0/stage/lib 
Undefined symbols:
  "boost::re_detail::get_mem_block()", referenced from:
      boost::re_detail::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::extend_stack()in main.o
  "boost::match_results<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > …
Run Code Online (Sandbox Code Playgroud)

c++ macos boost build-process boost-regex

4
推荐指数
1
解决办法
2981
查看次数

使用boost :: regex获取sub-match_results

嘿,让我说我有这个正则表达式: (test[0-9])+

我将它与之匹配: test1test2test3test0

const bool ret = boost::regex_search(input, what, r);

for (size_t i = 0; i < what.size(); ++i)
    cout << i << ':' << string(what[i]) << "\n";
Run Code Online (Sandbox Code Playgroud)

现在,what[1]将是test0(最后一次出现).让我们说我需要得到test1,2和3:我该怎么办?

注意:真正的正则表达式非常复杂,并且必须保持一个整体匹配,因此将示例正则表达式更改为(test[0-9])不起作用.

c++ regex boost boost-regex

4
推荐指数
2
解决办法
9822
查看次数

如何使用Boost.Regex获取正则表达式匹配值?

我正在尝试从URL中提取域名.以下是一个示例脚本.

#include <iostream>
#include <string>
#include <boost/regex.hpp>

int main () {

  std::string url = "http://mydomain.com/randompage.php";
  boost::regex exp("^https?://([^/]*?)/");
  std::cout << regex_search(url,exp);

}
Run Code Online (Sandbox Code Playgroud)

如何打印匹配值?

c++ regex boost-regex

3
推荐指数
1
解决办法
3790
查看次数

c ++使用boost正则表达式匹配的Url Parser

我如何使用boost regex解析c ++中的url,就像我有一个url一样

http://www.google.co.in/search?h=test&q=examaple
Run Code Online (Sandbox Code Playgroud)

我需要拆分base url www.google.com然后查询路径search?h=test&q=examaple

c++ url boost boost-regex

3
推荐指数
1
解决办法
8579
查看次数

将 boost::regex_search() 与字符串迭代器一起使用

我试图让 boost::regex 为我提供搜索字符串中出现的所有模式。认为这样的事情会很简单,但是让 boost 和 STL 在所有内容之上添加 10 个元层模板混淆:)。

我最近的尝试是使用 regex_search(),但不幸的是我的调用似乎与任何重载都不匹配。这是一个超级蒸馏的例子:

std::string test = "1234567890";
boost::regex testPattern( "\\d" );
boost::match_results<std::string::const_iterator> testMatches;
std::string::const_iterator startPos = test.begin();
while( regex_search( startPos, test.end(), testMatches, testPattern ) ) {
    // Do stuff: record match value, increment start position
}
Run Code Online (Sandbox Code Playgroud)

我对 regex_search() 的调用会触发智能感知,并且无法编译(“没有'regex_search'实例与参数列表匹配”)。

我试图调用的重载是:

template <class BidirectionalIterator,
    class Allocator, class charT, class traits>
bool regex_search(BidirectionalIterator first, BidirectionalIterator last,
    match_results<BidirectionalIterator, Allocator>& m,
    const basic_regex<charT, traits>& e,
    match_flag_type flags = match_default );
Run Code Online (Sandbox Code Playgroud)

这似乎与我的调用相匹配。

任何想法表示赞赏!以及做此类事情的替代方法。我最终想要做的是将字符串拆分为:

"0.11,0.22;0.33,0.444;0.555,0.666"
Run Code Online (Sandbox Code Playgroud)

进入浮点字符串的组成列表,然后我可以解析它。

在任何其他正则表达式包中,这都很简单 - …

c++ regex boost-regex

3
推荐指数
1
解决办法
5995
查看次数

使用boost :: regexp忽略大小写

奇怪的是,谷歌拒绝回答这样一个简单的问题:
我如何使boost :: regexp不区分大小写?

这就是我所拥有的:

static const boost::regex bad_words("(?:^|.* )(f(?:uc|a)k(?:i[ng]{1,2})?|bitch(?:es|iz)?)(?:$| .*)");   //reduced to the english ones
Run Code Online (Sandbox Code Playgroud)

当然,我也希望过滤大写的坏词.这就是我匹配它们的方式:

//std::string ms; - chat messsage
//boost::match_results<std::string::const_iterator> results;  - prewious regexp results
else if(boost::regex_match(ms, results2, bad_words)) {   //
        std::stringstream msg;
        msg<<"Avoid bad words! Word '"<<results2[1]<<"' is banned!";
        this->whisper(results[1], msg.str());   //name, message
}
Run Code Online (Sandbox Code Playgroud)

那么,对于不敏感的正则表达式还有另一个函数吗?还是另一个正则表达式对象?或修改器i可用?

c++ case-insensitive boost-regex

3
推荐指数
1
解决办法
4936
查看次数

查找reg ex中的特定单词以及特殊字符

string emailBody = " holla holla testing is for NewFinancial History:\"xyz\"  dsd  NewFinancial History:\"abc\"  NewEBTDI$:\"abc\"  dsds  ";

   emailBody = string.Join(" ", Regex.Split(emailBody.Trim(), @"(?:\r\n|\n|\r)"));
                var keys = Regex.Matches(emailBody, @"\bNew\B(.+?):", RegexOptions.Singleline).OfType<Match>().Select(m => m.Groups[0].Value.Replace(":", "")).Distinct().ToArray();
                foreach (string key in keys)
                {
                    List<string> valueList = new List<string>();
                    string regex = "" + key + ":" + "\"(?<" + GetCleanKey(key) + ">[^\"]*)\"";

                    var matches = Regex.Matches(emailBody, regex, RegexOptions.Singleline);
                    foreach (Match match in matches)
                    {
                        if (match.Success)
                        {
                            string value = match.Groups[GetCleanKey(key)].Value;
                            if (!valueList.Contains(value.Trim()))
                            {
                                valueList.Add(value.Trim()); …
Run Code Online (Sandbox Code Playgroud)

c# regex boost-regex regex-greedy regex-lookarounds

3
推荐指数
1
解决办法
137
查看次数