views:

36

answers:

1

Hi,

I am using the Boost library under c++ to parse a load of text. I seem to be having a problem with this string

text text text Seat 6: player_name (11,111) text text text

I am trying to match on the middle bit, the seat number can be anything between 1 and 9, the player_name can be any character a-zA-Z and include numbers *_. and the value in the brackets can be 0 to 1,000,000 and should include ','

I have this regex which works in c# but won't work under boost, its not matching and I can't see why as in my RegexBuilder its coming back as correct:

Seat (?<seat_id>[0-9]): (?<player_name>[A-Z0-9]{1,}) (?<stack_size>\([0-9,]{1,}\))

Any ideas? I am going to leave out the <> because I am just going to match on the string and not worry about the individual values.

Thanks, R. Happy holloween.

A: 

You say the player name can include lowercase letters, but that's not what your code says. The code says player_name can only be numbers and uppercase letters. Perhaps in your C# code you set a flag to omit case-sensitivity from the check. In Boost, that's regex_constants::icase; use it when creating the regex object.

Also, remember that regex_match requires the expression to match the entire input string. If you want to know whether the regular expression matches anywhere in the input, then use regex_search instead.

Rob Kennedy
Thank you, the icase information was very useful. For some reason it fails on the last part of the regex, it doesn't seem to like \([0-9,]{1,0}\) and I am not sure where I am going wrong. It could be a value from 100 to 1,000,000. For some reason I can't get it to match and don't know why. Ah, also regex_search was also what I was looking for... thanks.
flavour404
You didn't write `{1,0}` before. Your only said `{1,}`. This new value means you want at least one digit, but not more than zero. That obviously never matches anything. If you want to say "one or more," then it's common to just write `+`. I'm concerned that you don't actually know what your goal is since you're so inconsistent in describing it. (Seat number should be "anything between 1 and 9," but the regex says `[0-9]`. Stack size "can be 0 to 1,000,000," or "from 100 to 1,000,000." The capitalization. The digit count.) Take a step back to figure out what you need. *Then* write the code.
Rob Kennedy