Trouble with C++ Regex POSIX character class

倖福魔咒の 提交于 2019-12-02 03:32:43

问题


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.


回答1:


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";



回答2:


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

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



回答3:


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.



来源:https://stackoverflow.com/questions/27281897/trouble-with-c-regex-posix-character-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!