Help using Regex

Hi, LeafyCircuits here!

Comp Specs:
OS: Windows 8 64-bit
IDE: Code::Blocks v13.12
Compiler: TDM-GCC v4.7.1 (MinGW)

I've had a recent need for Regex in an experiment of mine, but I just can't seem to figure out the ECMAScript syntax. Here's what I'm trying to accomplish:

I want the user input to be: any number of digits, one space, and any number of digits again. Ex.: "2 3", "34 58" "234234 2341098"

So, I assumed that the Regex pattern would be:
"\d+ \d+.*"

But when I call regex_match(), it yields false every time. I've tried so many variations on this expression, from grouping to using POSIX groups and ranges, but nothing matches.

Can anyone point me out on how to use ECMAScript, or at least to a tutorial (which I've also looked up, but none helped).

I will also say that for some reason, whenever I try to use a character class, or anything using square brackets '[' ']', Regex throws `error_brack'. I'm not sure entirely why that is.
Shouldn't it be "\\d+\\s{1}\\d+" [edit]or "\\d+\\s{1}\\d+\\n*"[/edit] Anyways, I was under the impression that GCC didn't have a full support for regex. You may have to use clang (compiler) or boost (library/header).

[edit] Here is a ECMAScript reference from this site: http://www.cplusplus.com/reference/regex/ECMAScript/
Last edited on
I believe g++-4.9 has full regex support. I'm not sure if there's a way of getting that on Windows without building. But building g++ is relatively painless these days so long as you can read the directions :)


Anyways, this seems to work: \d+[^\S\n]\d+

In action: http://regexr.com/394rq

PS,
regexr.com is amazing tool for developing regexes
Last edited on
Also note that regex_match() attempts to match the entire expression. Chances are you really want to be using regex_search().

Hope this helps.
Duoas said:
Also note that regex_match() attempts to match the entire expression. Chances are you really want to be using regex_search().


I've considered that, but I'm certain that I want the input to ONLY be what I've stated already. If I use regex_search(), if the user types anything else, it will still return true, which is not my goal.

giblit said:
I was under the impression that GCC didn't have a full support for regex.


I'm starting to think that's the case because whenever I use brackets in a Regex Pattern, regex throws exceptions, even when I know for a fact I have matching brackets. I've been salivating over the Boost libraries for awhile now, only daunted by the manual building. I suppose now is time to download it :P

Thanks everyone for your help an opinion :)
Last edited on
IIRC, GCC's regex support is an incomplete implementation.

Designing a regex engine is notoriously difficult. It is worth your time to do one of the following:
- use an existing regex C library (such as PCRE or the old GNU regex)
- compile Boost's regex, which is very complete and (AFAIK) relatively bug-free

I'm not really sure why GCC maintainers have such difficulty just using Boost's regex. (I'm sure there is a reason, whether valid or not, only I've not cared to find it out.)
You're using an outdated version of G++. Grab version 4.9 if you want real regex support, as 4.9 has everything but regex_traits::transform_primary implemented.
Ah, sweet. I'll update my GCC as soon as I get home next week.
Topic archived. No new replies allowed.