C++ 정규 표현식 (Regular Expressions)

2021. 3. 29. 23:58·C++/Library

정규 표현식 (Regular Expressions)

  • <regex> 라이브러리

    • http://www.cplusplus.com/reference/regex/
  • ECMAScript Grammar 사용

    • http://www.cplusplus.com/reference/regex/ECMAScript/

기본 예제

#include <iostream>
#include <regex>

int         main()
{
    using namespace std;

    regex   re("\\d");
    //regex   re("[ab]");
    //regex   re("[[:digit:]]{3}");
    //regex   re("[A-Z]+");
    //regex   re("[A-Z]{3}");
    //regex   re("[A-Z]{1, 5}");
    //regex   re("([0-9]{1})([-]?)([0-9]{1,4})");
    //regex   re("([0]{1}[1]{1}[0-9]{1})([-]?)([0-9]{3,4})([-]?)([0-9]{4})");
    //regex   re("([[:digit:][:alpha:]]+)([@]{1})([[:digit:][:alpha:]]+)([.]{1})([[:alpha:]]+)");

    string  str;

    while (true)
    {
        getline(cin, str);

        if (std::regex_match(str, re))
            cout << "Match\n";
        else
            cout << "No match\n";

        auto begin = std::sregex_iterator(str.begin(), str.end(), re);
        auto end = std::sregex_iterator();
        for (auto itr = begin; itr != end; ++itr)
        {
            std::smatch match = *itr;
            cout << match.str() << ' ';
        }
        cout << endl;
    }
}

/* stdin
1
2
123
*/

/* stdout stderr
Match
1
Match
2
No match
1 2 3
*/
저작자표시 (새창열림)

'C++ > Library' 카테고리의 다른 글

C++ 파일 임의 위치 접근  (0) 2021.03.29
C++ fstream  (0) 2021.03.29
C++ 흐름 상태 (Stream States)  (0) 2021.03.29
C++ cout  (0) 2021.03.29
C++ ostream  (0) 2021.03.29
'C++/Library' 카테고리의 다른 글
  • C++ 파일 임의 위치 접근
  • C++ fstream
  • C++ 흐름 상태 (Stream States)
  • C++ cout
Caniro
Caniro
  • Caniro
    Minimalism
    Caniro
  • 전체
    오늘
    어제
    • 분류 전체보기 (317)
      • Algorithm (13)
        • 알기 쉬운 알고리즘 (10)
        • Search (1)
        • Sort (2)
      • Arduino (0)
      • C++ (185)
        • Class (46)
        • Exception (6)
        • Library (51)
        • Overloading (10)
        • SmartPointer (5)
        • Syntax (33)
        • TBC++ (23)
        • Templates (9)
        • VisualStudio (2)
      • Embedded (1)
      • Git (4)
      • Java (5)
      • Linux (16)
        • Error (1)
        • Linux Structure (11)
      • MacOS (7)
      • OS (1)
        • Concurrency (1)
      • Python (21)
        • Class (1)
        • Function (2)
        • Syntax (17)
      • Raspberrypi (9)
      • Review (1)
      • Utility (12)
        • VSCode (5)
        • VirtualBox (3)
      • Web (8)
        • Nginx (1)
        • React (3)
        • Django (1)
      • Windows (20)
        • Registry (3)
        • WSL (1)
        • DeviceDriver (6)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    EXCLUDE
    java
    그림판
    윈도우 명령어
    윈도우
    logi options
    맥북 카카오톡 알림 안뜸
    MacOS
    KakaoTalk
    백기선
    dism
    Solaris 10
    스프링
    spring
    SFC
    citrix workspace
    windows
    시스템 복구
    Windows 11
    Workspace
    제외
    vscode
    mspaint
    SunOS 5.1
    알림
    스프링 프레임워크 핵심 기술
    로지텍 마우스 제스처
    unix
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
Caniro
C++ 정규 표현식 (Regular Expressions)
상단으로

티스토리툴바