Regex
i want to find the position of the first string in input.txt that matches a regex
// Input.txt
1 2 3
|
[A-Z]o
Hello everyone, i am John
It is rainy today
|
// Here is my code
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
string temp; // Store "Hello everyone, i am John
//It is rainy today"
temp.resize(500);
char REGEX[500];
FILE *fi;
// Openfile-------------------------------------
fi = fopen("input.txt","rt");
if (fi == NULL)
{
cout << "Cant open file input.txt";
return;
}
fscanf(fi,"%[^\n]\n",®EX);
regex rg (REGEX); // regex ->>> string rg
string regex = REGEX;
// Store data to temp
char s;
int i = 0;
int ntemp = 0; // temp elements number
while (!feof(fi))
{
if (fscanf(fi,"%c",&s) > 0)
temp[i] = s;
i++;
ntemp++;
}
temp[ntemp] = '\0';
// Check
string check = "";
check.resize(500);
for (int i = 0; i < ntemp - regex.length(); i++)
{
int pos ;
for (int j = 0; j < regex.length() ; j++)
{
pos = j;
check = check + temp[j];
}
if (regex_match(check,rg))
{
cout << pos;
return;
}
}
|
Last edited on
Someone help!!!! It not run
Last edited on
Is this the complete code? What is the message?
If possible post complete code including what include files you are using, etc.
The only thing i need now is the code of finding the first string that matches the REGEX...
--->how to check a small string in the big one with REGEX???
example:
regex: "[A-Z]o"
and the string Big is "Hello everyone, I am John. It is rainy today"
code:
|
Compare "Bi" with the regex "[A-Z]o", then compare "ig" with the regex,... so on... THat!!! i need that
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
#include <string>
#include <iostream>
#include <regex>
int main()
{
std::string expression_text = "[A-Z]o";
std::string text = "Hello everyone, i am John\nIt is rainy today";
std::regex expr(expression_text);
std::cout << "Regular expression: \"" << expression_text << "\"\n";
std::cout << "Searchable text:\n\"" << text << "\"\n";
const std::sregex_iterator beg(text.begin(), text.end(), expr), end;
if (beg == end)
std::cout << "No matches found.\n";
else
std::cout << "Matches:\n";
for (auto it = beg; it != end; ++it)
std::cout << '\t' << it->str() << '\n';
}
|
Thank you!!!!
it->position() <- we have the first position!!! great!!1
Topic archived. No new replies allowed.