开发者

Boost:regex_search - match string between brackets

given this part of a LogString:

... HTTP/1.1" 206 391402 "-" "AppleCoreMedia/1.0.0.8B117 (iPod; U; CPU OS 4_1 like Mac OS X; de_de)"

I need to match the last substring that is inside the brackets to get the Remote System Info from the string.

I'm not really into regular expressions but from my understanding I have to escape the brackets like "\(" and "\)".

Now to match the whole string I thought I'd have to do it like:

\(\.*\) with the dot matching anything and "*" for specifying a random amount of dots.

But it does't work. Neither does (\.*) or (.*) as you would expect.

There must be a problem with opening and closing the brackets because I get a Seg Fault when trying to match t开发者_如何学Gohe string this way.

Can anyone help me please?


Ok sorry for bothering you. This did the trick:

boost::regex f_sourceDir(conf.pString("filter_SourceFiles")),
        f_string(conf.pString("filter_String")),
        m_first(conf.pString("field0")),
            m_second("\\(([^)]+)\\)\"$",boost::regex::perl),
        m_third(conf.pString("field2")),
        m_fourth(conf.pString("field3"));

Seems to be a problem with parsing the configuration, it can't handle such strings. I think I have to change the Config class...

Thx anyway!


Try this

\(.*\)

See it here on Regexr

You need only to escape the brackets, but not the .. If you escape the . then you will match a literal dot and this will not be found.

If you not want to have the brackets then here is a solution using a capturing group

 \((.*)\)

See it here on Regexr

You find the part inside the brackets stored in the capturing group 1.


Just in case, there are two ways of handling a string with a regex: matching and searching. Matching either matches the whole string or fails, searching does not have to match the whole string. What you are doing is searching.

The expression you need using Perl syntax (boost::regex let's you choose the syntax) is something like:

\(([^)]+)\)"$

That is: find a substring of one or more non-right-bracket symbols within brackets followed by the trailing " and the end of the string.

In C++ you need to quote this expression:

boost::regex re("\\(([^)]+)\\)\"$", boost::regex::perl);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜