regex problem

Hello,when compile code i have problem
compiler say's ... srd::regex_error aborted(core dumped)
any idea why?????????
Also regex_replace dont recognize ...


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// regex_replace example
#include <iostream>
#include <string>
#include <regex>
#include <iterator>

int main ()
{
  std::string s ("there is a subsequence in the string\n");
  std::regex e ("\\b(sub)([^ ]*)");   // matches words beginning by "sub"

  // using string/c-string (3) version:
  std::cout << std::regex_replace (s,e,"sub-$2");

  // using range/c-string (6) version:
  std::string result;
  std::regex_replace (std::back_inserter(result), s.begin(), s.end(), e, "$2");
  std::cout << result;

  // with flags:
  std::cout << std::regex_replace (s,e,"$1 and $2",std::regex_constants::format_no_copy);
  std::cout << std::endl;

  return 0;
}

Last edited on
The GNU implementation did not have <regex> support before version 4.9.

For reasons that I have never been able to appreciate, their earlier versions compiled code using <regex> without so much as a murmur, and then their library threw regex_error (for well-formed constructs) at the unsuspecting programmer.

http://coliru.stacked-crooked.com/a/230e53461d072d0c
http://rextester.com/MDSG34462
i just upgrade gcc to gcc ver 4.9.
Same problem ...
1
2
3
4
5
6
7
8
wstring s=L"Αγοράσαμε από τον παντοπώλη λάδι πού αξίζει 245,8 δρχ. σαπούνι πού αξίζει 94 δρχ. και ρύζι πού αξίζει 11,75 δρχ. Πόσα ρέστα θα πάρωμε από τα 7/8 των 50 δρχ.;";

    std::wregex e1(L"(0-9)(.*)");
    std::wregex e2(L"(0-9)");

    std::wregex id(L"0[xX]");

    if (std::regex_match(s,e1))std::cout << "string literal matched\n";


But if remove [] and

std::wregex id(L"0xX"); all are ok...
no problem..

I am in last Ubuntu and CodeBlocks. Must i reinstall CodeBlocks?

Last edited on
I'm not sure your regular expressions are doing what you think...

"(0-9)" matches the sequence "0-9"

whereas "[0-9]" matches any single digit in '0'..'9'.

So if you want to find a number, match "[0-9]+" -- that's one or more digits.

What are you trying to find with "0[xX]" -- a C-style hexadecimal number?
What are you trying to find ... Only for the compilation ...
Change it with ...

std::wregex id(L"[0-9]");

i have the same error... srd::regex_error aborted(core dumped)

Last edited on
Seems to work fine on Ubuntu + GCC 4.9
http://coliru.stacked-crooked.com/a/59413acf44b28ad9
Ok, updating to gcc 4.9 all are ok. C::B works fine.
But my problem continues. In code i take for 11,75 the a1175 and all are ok.

1
2
3
4
5
6
std::wstring str = L"We bought the grocer oil worth 2,48 drachmas. "
"soap worth 94 drs. and rice worth 11,75 drachmas."
"How much change will take from 7/8 of 50 drachmas?";
std::wregex num(L"\\b([0-9]*),([0-9]*)");
std::wcout<<std::regex_replace(str,num,L"a$1$2")<<"\n";



But trying get all mutching strings sm size is 0. But why sm gives 0, after the result in replace is correct?

1
2
3
4
    std::wsmatch sm;
    std::regex_match(str,sm,num);
    std::wcout<<sm.size()<<std::endl;
    for (unsigned i=0; i<sm.size(); ++i)std::wcout << "[" << sm[i] << "] ";


Ah, you're using regex_match() when you should be using regex_search().

regex_match() attempts to match the entire expression...

which it will fail to do in your case, because you are trying to find a sub-expression that matches your string. Use regex_search() to find sub-expressions.
std::regex_iterator can be used to iterate over the sequence of results of repeated calls to std::regex_search
Last edited on
Thank's very mutch. Now code runs fine.
Last edited on
Trying to catch only 2,48 and 11,75 i take only [2,48] [2] [48]
Where is the error?
At replace is ok ..

1
2
3
4
5
6
7
8
9
std::wstring str = L"We bought the grocer oil worth 2,48 drachmas. "
"soap worth 94 drs. and rice worth 11,75 drachmas."
"How much change will take from 7/8 of 50 drachmas?";
std::wregex num(L"\\b([0-9]*),([0-9]*)");
std::wcout<<std::regex_replace(str,num,L"a$1$2")<<"\n";
std::wsmatch sm;
std::regex_search(str,sm,num);
std::wcout<<sm.size()<<std::endl;
for (unsigned i=0; i<sm.size(); ++i)std::wcout << "[" << sm[i] << "] ";
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <regex>
#include <string>
#include <iostream>
 
int main()
{
    const std::wstring str = L"We bought the grocer oil worth 2,48 drachmas. "
                              "soap worth 94 drs. and rice worth 11,75 drachmas."
                              "How much change will take from 7/8 of 50 drachmas?"; 
                              
    std::wregex num(L"\\b([0-9]*),([0-9]*)");
    
    auto iter = std::wsregex_iterator( str.begin(), str.end(), num );
    const auto end = std::wsregex_iterator();
 
    for( ; iter != end; ++iter) std::wcout << iter->str() << L'\n' ;
}

http://coliru.stacked-crooked.com/a/641545ada0ed660e
Thank you very mutch.
A last question... I am trying to catch only integers.
From 2,48 94 11,75 7/8 50 and 35 6/9, i must catch only 94 and 50.
Trying \\b([0-9]+) or \\b([0-9]*) etc... i have problem.
Any idea for regular expression?
std::wregex integer( L"\\s[0-9]+\\s" ); // whitespace, one or more decimal digits, whitespace
http://coliru.stacked-crooked.com/a/1801a9e6051cad0d
Topic archived. No new replies allowed.