assert with regular expression
Nov 29, 2016 at 11:47am UTC
I am attempting to test a regular expression entered by the user.
The format of the entry must comply with "|number|", for example |12| would pass, whereas |12a| would fail.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#include <iostream>
#include <cassert>
#include <string>
#include <regex>
int main() {
std::string s = "" ;
std::regex reg_expr("|[[:digit:]]|" );
std::cin >> s;
std::cout << "\nResult string : " << s;
assert(regex_match(s,reg_expr));
std::cout << "\nResult string : " << s;
return 0;
}
The assertion fails though, and I am unsure why.
Runtime assert failure appears as follows:
1 2 3 4
|12|
assert_test_concatenated_str: assert_test_concatenated_str.cpp:15: int main(): Assertion `regex_match(s,reg_expr)' failed.
Result string : |12|Aborted (core dumped)
Is it the regular expression or something else I am doing incorrectly?
Nov 29, 2016 at 12:05pm UTC
I think you need to use + if you want to allow more than one digit.
std::regex reg_expr("|[[:digit:]]+ |" );
Nov 29, 2016 at 12:11pm UTC
still fails though, with + added.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#include <iostream>
#include <cassert>
#include <string>
#include <regex>
int main() {
std::string s = "" ;
std::regex reg_expr("|[[:digit:]]+|" );
std::cin >> s;
std::cout << "\nResult string : " << s;
assert(regex_match(s,reg_expr));
std::cout << "\nResult string : " << s;
return 0;
}
failure
1 2 3 4 5
|12|
assert_test_concatenated_str: assert_test_concatenated_str.cpp:15: int main(): Assertion `regex_match(s,reg_expr)' failed.
Result string : |12|Aborted (core dumped)
Nov 29, 2016 at 12:25pm UTC
ah, the bars are part of your input.
| has a special meaning in regex, you need to escape them R"(\|[[:digit:]]+\|)"
Nov 29, 2016 at 12:35pm UTC
It works when I do not use escaped "|" characters, but when the escaped bar is used, it fails.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#include <iostream>
#include <cassert>
#include <string>
#include <regex>
int main() {
std::string s = "" ;
std::cin >> s;
std::cout << "\nResult string : " << s;
// std::regex reg_expr("<[[:digit:]]+:"); // this works
std::regex reg_expr("\|[[:digit:]]+\|" );
assert(regex_match(s,reg_expr));
std::cout << "\nResult string : " << s;
return 0;
}
In addition, I get the following compile warning message :
1 2 3 4 5 6 7 8
c++ -g -std=c++14 -pedantic -Wall -Wpointer-arith -Wwrite-strings -Wcast-qual -Wcast-align -Wformat-security -Wformat-nonliteral -Wmissing-format-attribute -Winline -funsigned-char "assert_test_concatenated_str.cpp" (in directory: /c++/learn_c++)
assert_test_concatenated_str.cpp: In function ‘int main()’:
assert_test_concatenated_str.cpp:15:23: warning: unknown escape sequence: '\|'
std::regex reg_expr("\|[[:digit:]]+\|" );
^
assert_test_concatenated_str.cpp:15:23: warning: unknown escape sequence: '\|'
Compilation finished successfully.
Although it still successfully compiles, the run time assert error appears:
1 2 3 4
|12|
assert_test_concatenated_str: assert_test_concatenated_str.cpp:15: int main(): Assertion `regex_match(s,reg_expr)' failed.
Result string : |12|Aborted (core dumped)
Nov 29, 2016 at 12:55pm UTC
Try this:
std::regex reg_expr("\\|\\d+\\|" );
Nov 29, 2016 at 1:35pm UTC
thanks, spot on
Nov 29, 2016 at 2:27pm UTC
Topic archived. No new replies allowed.