Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

C++ Standard Library Regular Expression Operations

Tech 1

Regular expression syntax utilizes specific characters to define search patterns:

  • \: Escapes a special character
  • *: Matches zero or more occurrences
  • +: Matches one or more occurrences
  • ?: Matches zero or one occurrence
  • {n,m}: Matches between n and m occurrences
  • .: Matches any single character
  • |: Logical OR between expressions
  • [abc]: Matches any character in the set
  • [^abc]: Matches any character not in the set
  • [a-z]: Matches characters within the range

Implementing pattern matching in C++ involves three primary stages:

  1. Constructing the pattern object:
std::string source = "Locate the IP: 192.168.0.1";
std::string pattern_str = R"(\d+\.\d+\.\d+\.\d+)";
std::regex ip_regex(pattern_str);
  1. Executing the algorithm and capturing the outcome:
std::smatch captures;
std::regex_search(source, captures, ip_regex);
  1. Accessing the matched data:
std::cout << "Detected: " << captures[0] << std::endl;

Core Library Types

The <regex> header providse two fundamental template instantiations for pattern definitions:

using regex = basic_regex<char>;
using wregex = basic_regex<wchar_t>;

To store the outcomes of matching operations, the library offers several aliases based on match_results:

using cmatch = match_results<const char*>;
using wcmatch = match_results<const wchar_t*>;
using smatch = match_results<string::const_iterator>;
using wsmatch = match_results<wstring::const_iterator>;

Full Sequence Validation

std::regex_match evaluates whether the entire input sequence conforms exactly to the specified pattern.

#include <regex>
#include <iostream>
#include <vector>

int main() {
    std::string decimal_rule(R"(^[-+]?([0-9]*\.[0-9]+|[0-9]+)$)");
    std::regex validator(decimal_rule);

    std::vector<std::string> inputs = {"42", "-3.14", "+0.5", "1e5", "abc"};

    for (const auto& item : inputs) {
        if (std::regex_match(item, validator)) {
            std::cout << item << " is a valid numeric format.\n";
        } else {
            std::cout << item << " is invalid.\n";
        }
    }
    return 0;
}

Partial Sequence Search

std::regex_search locates subsequences within the target text that satisfy the pattern, requiring no full-sequence conformity.

#include <iostream>
#include <string>
#include <regex>
#include <vector>

int main() {
    std::vector<std::string> logs = {
        "System error at 2023-10-05T14:30:00",
        "User login on 2023-10-06T08:15:22",
        "Routine check passed",
        "Alert triggered 2023-10-07T23:59:59"
    };... (rest of the JSON formatted markdown)
    ```
    Wait, let me make sure the JSON is properly escaped. I'll write the JSON carefully.
Tags: C++regex

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.