๐Ÿ“ Coding Test Study/C++

[C++][์ •๊ทœํ‘œํ˜„์‹] <regex> ํ•จ์ˆ˜

ibelieveinme 2021. 8. 22. 08:45
728x90

๋ฌธ์ž์—ด ๋ฌธ์ œ์—์„œ ์œ ์šฉํ•˜๊ฒŒ ์“ธ ์ •๊ทœํ‘œํ˜„์‹์„ ๊ณต๋ถ€ํ•ด๋ณด์ž.

(http://sweeper.egloos.com/2999049 ์‚ฌ์ดํŠธ๋ฅผ ์ฐธ๊ณ ํ•ด์„œ ์ ์—ˆ์Šต๋‹ˆ๋‹ค.)

 

· ํ—ค๋”ํŒŒ์ผ: <regex>

· ๊ด€๋ จ ํ•จ์ˆ˜: 

 1) regex_match: ์ •๊ทœ์‹์ด ์ „์ฒด ๋Œ€์ƒ ๋ฌธ์ž์—ด๊ณผ ์ผ์น˜ํ•˜๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ๋ฐ˜ํ™˜(bool)

 2) regex_search: ๋ฌธ์ž์—ด์ด ์ •๊ทœ์‹๊ณผ ๋งค์น˜๋˜๋Š” sub string์„ ํฌํ•จํ•˜๊ณ  ์žˆ๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ๋ฐ˜ํ™˜(bool)

 3) regex_replace: ์ •๊ทœ์‹ ํ‘œํ˜„์‹์„ replace ๋ฌธ์ž์—ด๋กœ ๊ต์ฒดํ•œ๋‹ค. ๊ต์ฒด๋œ ์ „์ฒด ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.(string)

 4) swap: ๋‘ basic_regex ๋˜๋Š” match_results ๊ฐœ์ฒด๋ฅผ ๊ตํ™˜ ํ•ฉ๋‹ˆ๋‹ค.(string)

 

· ์ƒ์„ธ ์„ค๋ช…

1) std::regex_match ๋Š” ์ „์ฒด ๋ฌธ์ž์—ด์ด ํŠน์ • ๊ทœ์น™(ํŒจํ„ด)์„ ๊ฐ–๊ณ  ์žˆ๋Š”์ง€ ๋งค์นญํ•ด๋ณผ ๋•Œ

- ์ฐธ๊ณ  ๋ ˆํผ๋Ÿฐ์Šค: http://msdn.microsoft.com/en-us/library/bb982821(v=vs.100).aspx

string str("SookKaRak@gmail.com"); 
regex pattern("(\\w+[\\w\\.]*)@(\\w+[\\w\\.]*)\\.([A-Za-z]+)");
smatch m;

if (regex_match(str, m, pattern)){
        for (size_t i = 0; i < m.size(); i++){
            cout << i << "๋ฒˆ์งธ : " << m[i] << endl;
        }
}
else{
    cout << "NOT MATCH" << endl;
}

regex_match(str, m, pattern): str๋ฌธ์ž์—ด์ด ์ •๊ทœ์‹ ํŒจํ„ด pattern๊ณผ ์™„์ „ํžˆ ๋งค์น˜ํ•˜๋Š”์ง€ ํ™•์ธํ•˜๊ณ  true or false๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค. ์ด ๋•Œ, ๊ทธ ๊ฒฐ๊ณผ ์ง‘ํ•ฉ์€ smatch ์„ ์‚ฌ์šฉํ–ˆ๋‹ค. ์ฆ‰, ()์•ˆ์˜ ๋‚ด์šฉ์„ ์ €์žฅํ•ด ๋‘๋Š”๋ฐ, m[0]์— ์ „์ฒด ๊ฒฐ๊ณผ ์ง‘ํ•ฉ์ด ๋“ค์–ด ์žˆ๊ณ , m[1] ~ m[3]๊นŒ์ง€ ๊ฐœ๋ณ„ ๊ฒฐ๊ณผ๊ฐ€ ๋“ค์–ด ์žˆ๋‹ค.

 

(์œ„ ์ฝ”๋“œ์˜ ํ•˜์œ„ ์ •๊ทœ์‹ ์„ค๋ช…)

โ‘  (\\w+[\\w\\.]*): ์ฒซ๋ฒˆ์งธ \๋Š” ์ •๊ทœ์‹ ์‹œ์ž‘์„ ๋œปํ•˜๊ณ , ๋‘๋ฒˆ์งธ \w+๊ฐ€ ์ •๊ทœ์‹ ํŒ๋ณ„ ๊ธฐํ˜ธ๋กœ ๋ฌธ์ž๊ฐ€ 1๊ฐœ ์ด์ƒ ์˜จ๋‹ค๋Š” ๋ง์ด๋‹ค. ๋’ค \\w๋Š” ๋ฌธ์ž 1๊ฐœ, \\.๋Š” .๊ธฐํ˜ธ๋ฅผ ์˜๋ฏธํ•˜๋Š”๋ฐ, []*๋กœ ํ‘œํ˜„๋˜์–ด ์žˆ์œผ๋ฏ€๋กœ w.์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋ชจ๋“  ๋ฌธ์ž์—ด์ด ์˜จ๋‹ค๋Š” ์˜๋ฏธ๋‹ค.

 

โ‘ก (\\w+[\\w\\.]*): ์ฒซ๋ฒˆ์งธ \๋Š” ์ •๊ทœ์‹ ์‹œ์ž‘์„ ๋œปํ•˜๊ณ , ๋‘๋ฒˆ์งธ \๊ฐ€ ๊ธฐํ˜ธ๋ฅผ ๋œปํ•˜๋Š”๋ฐ, ํŠน์ˆ˜๋ฌธ์ž ์•ž์—๋Š” ๋ฐ˜๋“œ์‹œ \๋ฅผ ์จ์ค˜์•ผ ํŠน์ •๋ฌธ์ž์—ด๋กœ ์ธ์‹ํ•œ๋‹ค. ์ฆ‰, ์ด ์ •๊ทœ์‹์˜ ์˜๋ฏธ๋Š” ํŠน์ˆ˜๋ฌธ์ž . or - or / ๊ฐ€ ์˜จ๋‹ค๋Š” ๋ง์ด๋‹ค.

 

โ‘ข \\.: ๋ฌธ์ž. ์ด๋ผ๋Š” ์˜๋ฏธ. ํŠน์ˆ˜๋ฌธ์ž ์•ž์—๋Š” \๊ธฐํ˜ธ๋ฅผ ๋ถ™์—ฌ์ค˜์•ผ ํŠน์ˆ˜๋ฌธ์ž๋กœ ์ธ์‹ํ•œ๋‹ค.

 

โ‘ฃ ([A-Za-z]+): A-Z or a-z ๋ฌธ์ž์—ด์ด ์˜ฌ๊ฑด๋ฐ, 1๊ฐœ ์ด์ƒ ์˜ฌ๊ฑฐ๋ž€ ์˜๋ฏธ.

์ฆ‰, \w|W ์™€ [A-Za-z]๋Š” ๊ฐ™์€๋ง์ด๋‹ค. ๊ธฐํ˜ธ๋ฅผ ์‚ฌ์šฉํ–ˆ๋ƒ, ์ง์ ‘ ์ ์–ด์คฌ๋ƒ์˜ ์ฐจ์ด์ผ ๋ฟ!

 

 

2) std::regex_search ๋Š” ๋ฌธ์ž์—ด๋‚ด์—์„œ ํŠน์ • ๋ฌธ์ž์—ด์„ ๊ฒ€์ƒ‰ํ•  ๋•Œ

void SearchTest(){
    string str("My email address is SooKkaRak@gmail.com");      
    regex pattern("(\\w+[\\w\\.]*)@(\\w+[\\w\\.]*)\\.([A-Za-z]+)");    
    smatch m;  
    
    if (regex_search(str, m, pattern)){          
        for (size_t i = 0; i < m.size(); i++){                      
            cout << i << "๋ฒˆ์งธ : " << m[i] << endl;          
        }      
        else{              
            cout << "NOT MATCH" << endl;       
        }
    }
}

์—ฌ๋Ÿฌ๊ฐœ๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์„ ๊ฒฝ์šฐ์—๋Š” while๋ฌธ์œผ๋กœ ๋‹ค์Œ ๋ถ€๋ถ„์„ ์ฐพ๊ฒŒ ๊ตฌํ˜„ ํ•œ๋‹ค.

void SearchTest(){
    regex pattern("(\\w+[\\w\\.]*)@(\\w+[\\w\\.]*)\\.([A-Za-z]+)");    
    string str("Primary:SooKkaRak@gmail.com     Secondary:Sweeper@yahoo.com");
 
    smatch m;  
    while (regex_search(str, m, pattern))      {          
        for (size_t i = 0; i < m.size(); i++)           {                      
            cout << i << "๋ฒˆ์งธ : " << m[i] << endl;          
        }              
        cout << endl;
 
        // ๋‹ค์Œ sub match๋ฅผ str๋กœ ์…‹ํŒ…
        str = m.suffix();      
    }
}

smatch ๊ฐ์ฒด์˜ suffix() ํ•จ์ˆ˜๋Š” pattern์— ์ ์€ ()์•ˆ์˜ ๋‚ด์šฉ์„ ์ €์žฅํ•ด ๋‘”๋‹ค. ๋ฌธ์ž์—ด์—์„œ ์ผ์น˜ํ•˜๋Š” ๋งˆ์ง€๋ง‰ ๋ถ€๋ถ„์˜ ์‹œํ€€์Šค๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” primary:~~~๋งค์นญ ํ›„, index๋ฅผ Primary:SooKkaRak@gmail.com๋์˜ ์ธ๋ฑ์Šค๋ฅผ ๊ฐ€์ ธ์™€์„œ str์— ๋‹ค์‹œ ๋„ฃ์œผ๋ฏ€๋กœ Secondary:~~~๋ฅผ ํƒ์ƒ‰ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด๋‹ค. 

 

match_results::suffix

match_results::suffix 02/04/2013 2 minutes to read In this article --> Gets sequence after last submatch. const_reference suffix() const; The member function returns a reference to an object of type sub_match that points to the character sequence th

docs.microsoft.com

 

 

3) std::regex_replace ๋Š” ๋ฌธ์ž์—ด์„ ์น˜ํ™˜ํ•  ๋•Œ

์ฐธ๊ณ  ๋ ˆํผ๋Ÿฐ์Šค: http://msdn.microsoft.com/en-us/library/bb982317(v=vs.100).aspx

void ReplaceTest(){
    string str("Primary:SooKkaRak@gmail.com     Secondary:Sweeper@yahoo.com");
    regex pattern("(\\w+[\\w\\.]*)@(\\w+[\\w\\.]*)\\.([A-Za-z]+)");    
 
    string result = regex_replace(str, pattern, string("$1@$2.net"));
    cout << result << endl;
}

regex_replace(str, pattern, string("$1@$2.net"): str ๋ฌธ์ž์—ด์—์„œ ์ •๊ทœ ํ‘œํ˜„์‹ pattern๊ณผ ์ผ์น˜ํ•˜๋Š” ๋ฌธ์ž์—ด์„ replace(3๋ฒˆ์งธ ์ธ์ž)์™€ ๊ต์ฒดํ•œ ํ›„, ์ „์ฒด ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

์•„๋ž˜ ์˜ˆ์ œ๋Š” ์ผ/์›”/์—ฐ๋„ ํ˜•์‹์˜ ๋‚ ์งœ๋ฅผ ์—ฐ๋„/์›”/์ผ์˜ ํ˜•์‹์œผ๋กœ ๋ฐ”๊พธ์–ด ์ค€๋‹ค.

y/m/d๊ฐ„์˜ ๋ธ๋ฆฌ๋ฏธํ„ฐ๋Š” /(์Šฌ๋ž˜์‰ฌ), -(ํ•˜์ดํ”ˆ), .(๋งˆ์นจํ‘œ) ์„ธ ๊ฐœ์ค‘ ํ•˜๋‚˜๊ฐ€ ๋  ์ˆ˜ ์žˆ๋‹ค.

string format_date(const string& date){
    // 5๊ฐœ์˜ ํ•˜์œ„ ํ‘œํ˜„์‹
    const regex pattern("(\\d{1,2})(\\.|-|/)(\\d{1,2})(\\.|-|/)(\\d{4})");
    // ์—ญ์ฐธ์กฐ๋ฅผ ์—ญ์ˆœ์œผ๋กœ ๋‚˜์—ดํ•˜๋Š” replacer
    string replacer = "$5$4$3$2$1";
 
    return regex_replace(date, pattern, replacer);
}
 
int main(){
    string date1 = "1/2/2008";
    string date2 = "12.08.2008";
 
    cout << date1 << " -> " << format_date(date1) << endl;
    cout << date2 << " -> " << format_date(date2) << endl;
}

regex_replace(date, pattern, replacer): () ๊ด„ํ˜ธ ํ•˜๋‚˜๋ฅผ ์™ผ์ชฝ๋ถ€ํ„ฐ $1, $2, $3, $4, $5๋กœ ํ‘œํ˜„ํ•˜๋Š”๋ฐ, $1 ~ $5๋กœ ํ‘œํ˜„๋œ ๋ฌธ์ž์—ด date๋ฅผ $5 ~ $1๋กœ ์ˆœ์„œ๋ฅผ ๋ฐ”๊พธ๊ฒ ๋‹ค๋Š” ์˜๋ฏธ๋‹ค.

 

(์œ„ ์ฝ”๋“œ์˜ ํ•˜์œ„ ์ •๊ทœ์‹ ์„ค๋ช…)

โ‘  \\d{1,2}: ์ฒซ๋ฒˆ์งธ \๋Š” ์ •๊ทœ์‹ ์‹œ์ž‘์„ ๋œปํ•˜๊ณ , ๋‘๋ฒˆ์งธ \d๊ฐ€ ์ •๊ทœ์‹ ํŒ๋ณ„ ๊ธฐํ˜ธ๋กœ ์‹ญ์ง„์ˆ˜ ์ˆซ์ž ํ•˜๋‚˜๋ฅผ ๋œปํ•œ๋‹ค. ๋’ค์— {}๋Š” ์•ž ์ •๊ทœ์‹ \d์˜ ๊ฐœ์ˆ˜๋ฅผ ์˜๋ฏธํ•œ๋‹ค. ์ฆ‰ ์ด ์ •๊ทœ์‹์˜ ์˜๋ฏธ๋Š” ์‹ญ์ง„์ˆ˜ ์ˆซ์ž 1~2๊ฐœ๊ฐ€ ์žˆ๋‹ค๋Š” ๋ง์ด๋‹ค.

 

โ‘ก \\.|-|/: ์ฒซ๋ฒˆ์งธ \๋Š” ์ •๊ทœ์‹ ์‹œ์ž‘์„ ๋œปํ•˜๊ณ , ๋‘๋ฒˆ์งธ \๊ฐ€ ๊ธฐํ˜ธ๋ฅผ ๋œปํ•˜๋Š”๋ฐ, ํŠน์ˆ˜๋ฌธ์ž ์•ž์—๋Š” ๋ฐ˜๋“œ์‹œ \๋ฅผ ์จ์ค˜์•ผ ํŠน์ •๋ฌธ์ž์—ด๋กœ ์ธ์‹ํ•œ๋‹ค. ์ฆ‰, ์ด ์ •๊ทœ์‹์˜ ์˜๋ฏธ๋Š” ํŠน์ˆ˜๋ฌธ์ž . or - or / ๊ฐ€ ์˜จ๋‹ค๋Š” ๋ง์ด๋‹ค.

 

โ‘ข \\d{4}: ์ˆซ์ž์ด๊ณ  4 ์ž๋ฆฌ ์ˆ˜๋ผ๋Š” ์˜๋ฏธ์ด๋‹ค.

 

 

· ๊ธฐํƒ€ ํ•˜์œ„ ํ‘œํ˜„์‹

์ •๊ทœ์‹

\\d* : ์ž„์˜์˜ ๊ฐœ์ˆ˜์˜ ์ˆซ์ž๋ฅผ ์˜๋ฏธ

\\d+: ์ˆซ์ž 1๊ฐœ ์ด์ƒ์„ ์˜๋ฏธ

\\d?: ์ˆซ์ž 0๊ฐœ or 1๊ฐœ ์ด์ƒ์ž„์„ ์˜๋ฏธ

\\d{3}: ์ˆซ์ž 3๊ฐœ๋ผ๋Š” ์˜๋ฏธ. ๋งŒ์•ฝ 1234๊ฐ€ ๋“ค์–ด์žˆ๋‹ค๋ฉด Matching or Search๊ฐ€ false๋ฅผ ๋ฆฌํ„ดํ•จ.

\d{3,4}: ๋ฌธ์ž์—ด์ด ์ˆซ์ž๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋Š”๋ฐ, 3 or 4์ž๋ฆฌ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค๋Š” ์˜๋ฏธ

 

[0-9]: 0-9๋กœ ์ด๋ฃจ์–ด์ ธ์žˆ๋‹ค๋Š” ์˜๋ฏธ

[01]{3}: 0ํ˜น์€ 1๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๊ณ , ํ•ด๋‹น ์ข…๋ฅ˜์˜ ๋ฌธ์ž๊ฐ€ 3๋ฒˆ ๋‚˜ํƒ€๋‚  ์ˆ˜ ์žˆ๋‹ค๋Š” ์˜๋ฏธ

 

\w*: ์ž„์˜์˜ ๋ฌธ์ž๋ฅผ ์˜๋ฏธ

\.: ๋ฌธ์ž .์„ ์˜๋ฏธ(๊ทธ๋ƒฅ .์“ฐ๋ฉด ์ž„์˜์˜ ๋ฌธ์ž๋กœ ๊ฐ™์ด ํ•ด์„๋จ)

 

[A-Za-z]+: A-Z์™€ a-z๊นŒ์ง€์˜ ์•ŒํŒŒ๋ฒณ์œผ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค๋Š” ์˜๋ฏธ

[A-Z]+: A-Z๊นŒ์ง€ ๋ฌธ์ž๊ฐ€ 1๊ฐœ ์ด์ƒ ์žˆ๋‹ค๋Š” ์˜๋ฏธ

[a-z]: a-z๋กœ ์ด๋ฃจ์–ด์ ธ์žˆ๋‹ค๋Š” ์˜๋ฏธ

[A-Z]{1,5}: A-Z๊นŒ์ง€์˜ ๋ฌธ์ž๊ฐ€ ์ตœ์†Œ 1๊ฐœ, ์ตœ๋Œ€ 5๊ฐœ ์กด์žฌํ•œ๋‹ค๋Š” ์˜๋ฏธ

 

 

· ๊ด€๋ จ๋ฌธ์ œ

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์ˆซ์ž ๋ฌธ์ž์—ด๊ณผ ์˜๋‹จ์–ด

๋„ค์˜ค์™€ ํ”„๋กœ๋„๊ฐ€ ์ˆซ์ž๋†€์ด๋ฅผ ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋„ค์˜ค๊ฐ€ ํ”„๋กœ๋„์—๊ฒŒ ์ˆซ์ž๋ฅผ ๊ฑด๋„ฌ ๋•Œ ์ผ๋ถ€ ์ž๋ฆฟ์ˆ˜๋ฅผ ์˜๋‹จ์–ด๋กœ ๋ฐ”๊พผ ์นด๋“œ๋ฅผ ๊ฑด๋„ค์ฃผ๋ฉด ํ”„๋กœ๋„๋Š” ์›๋ž˜ ์ˆซ์ž๋ฅผ ์ฐพ๋Š” ๊ฒŒ์ž„์ž…๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ์ˆซ์ž์˜ ์ผ๋ถ€ ์ž

programmers.co.kr

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์ˆœ์œ„ ๊ฒ€์ƒ‰

["java backend junior pizza 150","python frontend senior chicken 210","python frontend senior chicken 150","cpp backend senior pizza 260","java backend junior chicken 80","python backend senior chicken 50"] ["java and backend and junior and pizza 100","pyt

programmers.co.kr

 

728x90