Skip to content

Instantly share code, notes, and snippets.

@squeeve
Created October 27, 2023 08:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save squeeve/a39170250fc1eb9c13e48523275ffca9 to your computer and use it in GitHub Desktop.
Save squeeve/a39170250fc1eb9c13e48523275ffca9 to your computer and use it in GitHub Desktop.
Using regex to parse lines into components.
#include <iostream>
#include <regex>
#include <string>
using namespace std;
smatch matchy(string x) {
smatch m;
regex regexp("^Mem\\((\\d+)\\)=(\\d+)\n$");
regex_search(x, m, regexp);
return m;
}
int main() {
vector<string> all;
all.push_back("Mem(100)=120\n");
all.push_back("Mem(300)=420\n");
all.push_back("Mem(250)=620\n");
all.push_back("Mem(abc)=620\n");
for (auto i : all) {
cout << endl << "Original: " << i;
smatch m = matchy(i);
if (m.size() > 2) {
cout << "Pattern: first=" << m[1] << "\tsecond=" << m[2] << endl;
} else {
cout << "Nope, nothing." << endl;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment