STL String & Regex Flashcards

1
Q

How to replace string?

A
  1. find the position of start replacing
  2. specify size of characters to be replaced
  3. provide new string.

Example:

string data {“data 123 okay”};

auto pos = data.find( “123” );

data.replace( pos, sizeof(“123”) - 1, “56789” );

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How to create string from one char?

A

string s (1, ‘x’ );

or

string s{‘x’};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

string s1(“love”, 5 );

std::cout << s1.length();

what is the output?

A

5 it is “love\0”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Is following code work?

string sql = “Select * from Order “

“Where ID = ‘A001’ “

A

Yes and what is in the sql string is

“Select * from Order Where ID = ‘A001’ “

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is Raw string literals?

A

string stringInQuote = R”(This is a “string”)”;

R”( )”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

regex “ab+c” mean?

A

+ 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”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

regex “ab*c+” mean?

A

* mean zero or more

+ mean one or more
so the valid one will be: “ac” “accc” “abcccc”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does “regex_match” do? and what is its syntax?

A

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;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

In regex, what is () used for?

A

() 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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

In regex, what is “?”

A

0 or 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is regex for date?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How does regex_match work?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How does regex_search work?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

sregex_token_iterator is the typdef of?

A

regex_token_iterator

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is regex_iterator?

A

iterator of found match regex.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How to use regex_iterator?

A

const regex r(“\S+”); // anything not whitespace

string input(“aa bb cc dd”);

for (sregex_iterator i(input.begin(), input.end(), r), end; i != end; ++i) {

cout << (*i)[0] << endl;

}

the output is

aa bb cc dd

17
Q

Can we pass C-String to function that parameter is reference of string (string&)?

A

No because initial value of reference to non-const must be lvalue. the following is invalid syntax

void test (string& s);

test(“this is C-string”); // can’t compile

test( string(“This is rvalue”) // can’t compile

void test( const string& s);

test(“this is C-string”); // okay

test( string(“This is rvalue”) // okay