开发者

Getting spaces with Boost::regex

I have a chunk of text like:

Name=Long John Silver;Profession=cook;Hobby=Piracy

And using the Boost::regex library I am trying to get the value of each attribute so I have the following regular expression for Name:

regex(".*Name=([^;$]*).*")

I understand it as "get everything after Name= until you find a ;开发者_开发知识库 or the end of line". However what I am obtaining using regex_match is only "Long", no spaces. Of course I have been trying with several RE combinations but I am not able of working it out.

Any clue? Cheers.


No, inside a character class (the square brackets) the $ has no special meaning. It simply matches the literal '$'.

Try this regex:

Name=([^\s;]*)

The character class [^\s;] matches any character except a whitespace character and a semicolon. The name is now captured inside the first match-group.

EDIT:

And if you want to match the entire name "Long John Silver", use this regex:

Name=([^;]*)
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜