0

I've just started learning regex.

I need to match things like this:

hostname21.processName

The . is the anchor. Pre and post anchor are represented by [a-zA-Z0-9_] but I'm not sure how to match both of the conditions on either side of the anchor.

I'm using boost::regex if that helps.

EDIT

I tried Jerry's code with boost::regex reg("[a-zA-Z0-9_]+\\.[a-zA-Z0-9_]+"); and it worked. Thanks everyone.

Tyler Jandreau
  • 4,245
  • 1
  • 22
  • 47
  • Do you mean something like that: `[a-zA-Z0-9_]+\.[a-zA-Z0-9_]+`? – Jerry Sep 16 '13 at 18:00
  • That doesn't seem to work. – Tyler Jandreau Sep 16 '13 at 18:02
  • its much better to learn using regexes using some kind of regex testing tool. Can even be notepad++ if you dont want to use notepad then google regex testing websites. boost uses perl styled regular expressions - they are quite popular. if you want match text u just specified u could even write ".*\\..*" - if you really want to match dot. – Aleksander Fular Sep 16 '13 at 18:03
  • @AleksanderFular Notepad++ is actually a pretty decent idea, because it actually uses boost as it's regex engine (at least as of Notepad++ 6). And so does Sublime, I think. – Martin Ender Sep 16 '13 at 18:05
  • @TylerJandreau note that when you are using Jerry's code, in C++ code, you'll probably have to double-escape the period. So as a string it would look like: `"[a-zA-Z0-9_]+\\.[a-zA-Z0-9_]+"` And you might want to add (regex) anchors around the pattern like `"^...$"`. – Martin Ender Sep 16 '13 at 18:06

1 Answers1

0

I would use reg("(\w)+\\.(\w)+")

Quine
  • 158
  • 4