C++ Regex - how?

Hey,
I want to use C++ regex library, to use it in my lil' CLI game.
The aim is to create the template(not C++ template :)) for command(like "go north"). Then, if player writes anything that contains "go", and "north" appearing after that(examples: "go north" "please go north" "Let's go north!" "Princess, I insist that we go to the north") would all be interpreted as simple "go north".
I have used a bit of regex in Linux system, but not this ECMAScript regex that C++ uses.I've checked in some online regex tester, that if I write:
".*go.*north.*" then anything I want will be matched; however, I'd like these to be specifically separated by space(the rule is: between north and go there must be at least one character: a space, so that gornorth won't be interpreted as command.).
Bonus points: if it's possible to do that without some kind of black magic, I'd like to achieve following goals:
1)Before go, there can be any amount of characters, but if there is 1 or more characters, the character just before go must be space.
2)In between go and north, there must be at least one character, space. If there are more, then characters right after go and just before north have to be spaces.

Thanks in advance for help!
Try .*go(\s+.+\s+|\s+)north.*/i
it is words "go" and "north" separated either by 1+ spaces or 1+ spaces, any amount of characters, 1+ spaces
/i means that checking is case insensitive. Go north will be accepted too
Last edited on
Thanks! So in that case, I should check whole string for match, instead of searching for pattern in string, or does it not matter? If it doesn't matter, is whole string matching more efficient?
No, pattern match would be fine. I just fixed your example, thinking that it is what you want. You can delete first and last .*
Hey, needed a little more help, but found answer before posting, so providing it for future generations.
First of all, your regex is almost good, but it doesn't work. I needed to fix few things:
".*go(\\s+.+\\s+|\\s+)north.*"
This is the resulting regex that I'm using now. As you can see, \s was just escaped character, which wasn't good. Changing it to \\s escaped the backslash instead.
Secondly, I discarded /i. I figured out that this was treated as part of text, instead of being regex token(or whatever it's called). After a bit more research, I found a way to achieve my goal, plus nice bonus for POSIX users.

What I needed was in regex constructor, after passing a regex, next arguments should be binary or'd flags from std::regex_constants namespace.
For example, to get regex above, working in ECMAScript standard and case-insensitive:
std::regex myRegex( ".*go(\\s+.+\\s+|\\s+)north.*", std::regex_constants::ECMAScript | std::regex_constants::icase);
Now as you see, you can feed it with ECMAScript flag, which basically tells it to use ECMAScript. I believe it's default behaviour, but you can get other regex standards. However, regex will look strange no matter what dialect you use :)
And last, but not least, you can set the behaviour of regex matching/searching as well. You use flags for it too, and they are listed in here: http://www.cplusplus.com/reference/regex/regex_constants/ .

Thanks for help!
Last edited on
. As you can see, \s was just escaped character, which wasn't good. Changing it to \\s escaped the backslash instead.
If you are going to write regexes in C++ ever, use raw literals. Or else amount of backslashes will quickly go out of hand.

R"(go(\s+.+\s+|\s+)north)"
http://en.cppreference.com/w/cpp/language/string_literal

Basicly my regex was in common form, how you feed it to regex contructor is another point.
Last edited on
There's website like:

http://regexr.com/

That allow you to play around with regular expressions. It's great for learning how they work exactly. But make sure you understand the limitations of (some) regular expression libraries. I use boost::regex myself because the more standard one lacks certain features (don't remember which)
Didn't know about string literal. Thanks for info :)
Topic archived. No new replies allowed.