0

Suppose I wanted to create a boost.regex expression that would match strings such as

"repetitions: 5 aaaaa" "repetitions: 3 aaa"

Is there a way to do this with boost?

Kvothe
  • 467
  • 4
  • 13

1 Answers1

0

I haven't used boost::regex in particular, but the regular expression you're asking for is straight-forward. Looking through the boost::regex docs, it looks like you'd do something similar to this:

boost::regex e("repetitions:\\s+\\d\\s+[a-zA-Z]+");

(double slashes are so the escape sequences aren't swallowed by the compiler)

Note: If you're also trying to validate that the first number matches the number of letter 'a', that won't work with just a regular expression. Regexes only match characters, without any clue as to what they mean, so '5' and '3' are not seen in any numerical way. Look at their docs on Captures. You'll want to get the number as a string, lexical_cast it to an integer, and use that to validate the aaaaa part.

Ari
  • 1,102
  • 9
  • 17
  • In your regex it will match things like "repetitions: 3 rrrr", which I don't think the OP meant – Daniel Sep 03 '12 at 17:31
  • I was thinking the same thing. I added a note on that regard. – Ari Sep 03 '12 at 17:47
  • Sorry I wasn't clear in my original post. I was "trying to validate that the first number matches the number of letter 'a'", as you put it. Thanks for the advice, I hadn't considered extracting the number, converting it to a string, then generating a new regex. Too bad there's not a more straightforward way to do it, though. – Kvothe Sep 03 '12 at 17:57
  • @Kvothe IMO there is a much more straightforward way to do it: read the number, and loop through the rest of the string while counting the repetitions. – R. Martinho Fernandes Sep 03 '12 at 18:00
  • @Fernandes The actual strings I want to analyze are more complicated - I was just trying to come up with a basic example of what I wanted. – Kvothe Sep 03 '12 at 18:14