Reputation: 878
I'm trying to do some input validation for a time in the format
"1:00 to 12:59 and AM/PM"
I created this regex:\b((1[0-2]|0?[1-9]):([0-5][0-9]) *([AaPp][Mm]))
.
I tested it at regexr.com and it worked fine.
However, when I try to do something like:
std::string s ("1:00 AM");
std::regex e ("\b((1[0-2]|0?[1-9]):([0-5][0-9]) *([AaPp][Mm]))");
if (std::regex_match (s,e))
std::cout << "string object matched\n";
It never works. Any idea what I might be doing wrong?
Upvotes: 2
Views: 166
Reputation: 17054
Use a raw string literal R
for using single \
. You can try like so:
R"(?!0:00|00:00)([0-1]?[0-2]:[0-5]\d\s*[AaPp][Mm])"
(?!0:00|00:00)
- Negative look ahead for 0:00 or 00:00 without consuming.
Demo:
https://regex101.com/r/t7Oa0Q/2
Upvotes: 2
Reputation: 878
Thanks to @MYGz I was able to fix this. Maybe it had something to do with escape characters.
All I did to change @MYGz's suggestion was to remove the \s.
It worked with ([0-1]?[0-2]:[0-5][0-9] *[AaPp][Mm])
Upvotes: 2