Regex problem

I am playing around with regex but it seems like my pattern is not working.


std::regex regex("^-?(0|[1-9]\\d*)[+\\-*/]-?(0|[1-9]\\d*)$")


What I was trying to do was have it as a basic calculator (it doesn't prevent division of 0 yet). Basically the pattern would be 0 or 1 '-' (sign), 0->infinity (excluding leading zeros such as 00001), '+' or '-' or '*' or '/' (operator), 0 or 1 '-' (sign), 0->infinity (excluding leading zeros)

So in other words:
signed/unsigned number operation signed/unsigned number

The problem seems to be with making sure there are no leading zeros so this bit:
1
2
3
(0|[1-9]\\d*)
//or
(0|[1-9][0-9]*)
from my understanding this means match 0 or 1-9 and 0-9(<-0 or more times)

can you not use ranges when using the alternate pattern? because if I do (0|1) it works fine but (0|[1-9]) doesn't and I'd prefer to not have to type each value manually instead of a range.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
#include <regex>

int main()
{
    const std::string test_cases[] = { "12-22", "+12 / 22", "+12345 * -32768", "+12*-0", "01 * 22", "123 **34", "+0++0" } ;
    
    const std::string zero_or_more_ws = R"(\s*)" ;  
    const std::string optional_sign = R"([+\-]?)" ;
    const std::string integer  = "(0|[1-9][0-9]*)" ; 
    const std::string oper = R"([+\-*/])" ;
    const std::string number = optional_sign + integer ;
    
    const std::regex expression( '^' + number + zero_or_more_ws + oper  + zero_or_more_ws + number + '$' ) ; 
    const std::regex ditto( R"(^[+\-]?(0|[1-9][0-9]*)\s*[+\-*/]\s*[+\-]?(0|[1-9][0-9]*)$)" ) ;  

    for( std::string s : test_cases )
    {
        std::cout << s << " -> " << std::boolalpha << std::regex_match( s, expression ) 
                  << " (" << std::regex_match( s, ditto ) << ")\n" ;
    }
}

http://coliru.stacked-crooked.com/a/b6198ef1f90ede9e
Very cool didn't realize you could put a R before and make it a raw string. Also I think it is a bit easier to read doing it your way. As far as the problem though I am not sure but it only works when I compile using clang and your flags clang++ -std=c++11 -stdlib=libc++ -O2 -Wall -pedantic-errors main.cpp -lsupc++ && ./a.out what exactly is libc++ and -lsupc++? also is there a way for me to compile it successfully with g++. I normally just use g++-4.8 -std=c++11 -Wall -Wextra -pendantic-errors main.cpp && ./a.out


http://coliru.stacked-crooked.com/a/7c29ad1f66ac1e3d

Edit after doing some reading around it seems like gcc4.8 doesn't fully support regex yet so guess you can't use alteration with ranges.
Last edited on
libc++ is the llvm/clang++ implementation of the standard library

libsupc++ is the gnu C++ language support library (RTTI, exceptions etc.) - the minimal library support required by freestanding implementations.

With g++, use boost regex. http://coliru.stacked-crooked.com/a/9623ce1f3613b58b
Last edited on
Oh alright cool thanks. I'll have to mess around with boost. I just was trying to test out the c++11 regex since the one in Qt is pretty nifty.
Topic archived. No new replies allowed.