AdmiralJonB
AdmiralJonB

Reputation: 2110

Trouble with C++ Regex POSIX character class

I'm trying to create a regex that is capable of analysing something like this:

002561-1415179671591i.jpg

The second part is a unix timestamp (before the i), and I need to extract that. I came up with the following syntax, but std::regex keeps throwing a regex_error before I even check for a match and I'm not too sure what's wrong.

Here's what I've got so far:([:d:])*-[[:d:]]*([:alpha:])\.jpg

The C++ code line that throws the error is the constructor to regex

std::regex reg(regex_expr);

where regex_expr is a string that has been read in from a file.

Really appreciate any help you can give.

Edit: I wrote a try catch section, and it seems I'm getting the following error code

std::regex_constants::error_brack

Edit 2: Ok... seems I'm still getting the error even with a cut-down test:

([:alpha:])*

Edit 3:

Seems I can't get any expression to work. Here's a bit more info. I'm using clang++ 3.5.0 on Kubuntu 14.04.

Upvotes: 3

Views: 510

Answers (3)

AdmiralJonB
AdmiralJonB

Reputation: 2110

After many tests, for whatever reason, I couldn't get this to work. As I had boost anyway, I tried out the boost::regex and it worked straight away, so I presume that something to do with either clang, or the version of the standard on this pc just wasn't working.

So simply put, tried boost and it worked straight away. Not much of an answer, but I guess that's how things go sometimes.

Upvotes: 0

Sceptical Jule
Sceptical Jule

Reputation: 917

I would rather use the perl-compatible syntax instead of character classes:

\d+-(\d+)[a-z]*\.jpg

Upvotes: 0

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89614

Not sure that [:d:] can stand for [:digit:]. [EDIT] (It seems it's possible)

When you use a POSIX character class, it must be enclosed in a character class like that:

[[:digit:]]

(This syntax allows to compose other classes [[:digit:]ab])

so:

std::string regex_expr = "([[:digit:]]*)-([[:digit:]]*)([[:alpha:]])\\.jpg";

or if you use the basic mode:

std::string regex_expr = "\\([[:digit:]]*\\)-\\([[:digit:]]*\\)\\([[:alpha:]]\\)\\.jpg";

Upvotes: 3

Related Questions