STL String & Regex Flashcards
How to replace string?
- find the position of start replacing
- specify size of characters to be replaced
- provide new string.
Example:
string data {“data 123 okay”};
auto pos = data.find( “123” );
data.replace( pos, sizeof(“123”) - 1, “56789” );
How to create string from one char?
string s (1, ‘x’ );
or
string s{‘x’};
string s1(“love”, 5 );
std::cout << s1.length();
what is the output?
5 it is “love\0”
Is following code work?
string sql = “Select * from Order “
“Where ID = ‘A001’ “
Yes and what is in the sql string is
“Select * from Order Where ID = ‘A001’ “
What is Raw string literals?
string stringInQuote = R”(This is a “string”)”;
R”( )”
regex “ab+c” mean?
+ mean 1 or more so the one that valid will be start with ‘a’ and ‘b’ many then end with ‘c’ Ex: “abbbbbc” or “abbc”
regex “ab*c+” mean?
* mean zero or more
+ mean one or more
so the valid one will be: “ac” “accc” “abcccc”
What does “regex_match” do? and what is its syntax?
To test the whole input string match the pattern or not. It return yes or no.
regex r(“ab*c+”);
if ( regex_match( input_string, r ) then do something;
In regex, what is () used for?
() parentheses is for precedence and capture group precedence “ax|yc” this is “ax” or “yc” so we can do “a(x|y)c” which is mean a then (x or y) and c capture group like “” then smatch the first group is in first and second group is in second or we can do “” the \d is \d for any digit the * for more. So if I do <34234><333> it is matched and first group is 34234 and second is 333
In regex, what is “?”
0 or 1
What is regex for date?
const regex r(“(\d{2}/\d{2}/\d{4})”); // this one is one capture group also with 3 capture groups const regex r(“(\d{2})/(\d{2})/(\d{4})”);
How does regex_match work?
regex_match( input_string, sub_match, regex ); the the whole string match then return true, false if not.
sub_match is smatch class, is the result of capture group. the first sub_match is the whole match and next is each sub group.
for (i : sub_match )
cout << i << endl; or
you can say
sub_match[0]; // the whole match
sub_match[1]; // capture group 1
How does regex_search work?
match part of the string.
the regex_match is for the whole string
regex_search( input_string, sub_match, regex );
sub_match.prefix() is the precedance that does not match sub_match.suffix() is the rest after match that does not match
sregex_token_iterator is the typdef of?
regex_token_iterator
What is regex_iterator?
iterator of found match regex.