views:

79

answers:

1

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.

A: 

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=([^;]*)
Bart Kiers
It is not working that way :(I want to obtain the complete string including spaces ("Long John Silver"). I have tried with Name=([^\s;]*) and Name=([^;]*) but I only obtain "Long". Thanks.
BasementGuy
See the edit. But you say you tried `Name=([^;]*)` and that this also captured only `"Long"`? I can't imagine that. I don't use this Boost regex engine, but all the regex engine I do use will capture the entire name with the regex `Name=([^;]*)`. Perhaps you forgot to recompile your code?
Bart Kiers
You were completely right, I was using the wrong algorithm (regex_match instead of regex_search) and also the wrong data source.Many thanks!
BasementGuy
Good to hear you fixed it!
Bart Kiers