CPP
cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <regex>
int main() {
std::string input = "int* ptr; // Pointer declaration";
// Define a regex pattern to match pointer declarations
std::regex pattern("\\b(\\w+)\\s*\\*\\s*(\\w+)\\b");
// Match the regex pattern against the input string
std::smatch match;
if (std::regex_search(input, match, pattern)) {
std::cout << "Type: " << match[1] << std::endl; // Matched type
std::cout << "Pointer name: " << match[2] << std::endl; // Pointer variable name
}
return 0;
}
/*
In this code, the regex pattern \\b(\\w+)\\s*\\*\\s*(\\w+)\\b is used to match pointer declarations. Here's what each part of the pattern means:
\\b: Word boundary, to ensure we're matching whole words.
(\\w+): Captures one or more word characters (letters, digits, or underscores), representing the type of the pointer.
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run