Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a few issues with my algorithm, trying to acquire the positions of regex matches in a string.
Please find the code here: https://godbolt.org/z/11q4Mo5fP
1) Why does the regex return 2 matches when there are 3,
2) The expected result is 0:2:4 when it returns 0:0

What I have tried:

Writing the algorithm to derive the expected result.
C++
#include <iostream>
#include <string>
#include <regex>

int main()
{
	std::vector<int> mval;

	int closed_val = 0;
	int open_val = 0;
    std::string dup_line = "} } }";
    std::smatch m;
    std::regex_search(dup_line, m, std::regex("([\\}]+)"));
    for (auto mv : m) std::cout << mv << std::endl;
    std::cout << m.size() << std::endl;
	closed_val = dup_line.find(m[0]);
	mval.push_back(closed_val);
	try {
		if (m.size() >= 1) {
			for (int j = 1; j < m.size(); j++) {
                std::cout << dup_line << std::endl;
				dup_line = dup_line.substr(dup_line.find(m[j]) + 1, dup_line.size() - 1);
                std::regex_search(dup_line, m, std::regex("([}]+)"));
                std::cout << dup_line << std::endl;
				int f = dup_line.find(m[j]);
				if (f == -1 || f > dup_line.size() - 1)
					continue;
                    std::cout << f << std::endl;
                for (auto mv : m) std::cout << mv << std::endl;
				std::cout << dup_line.find(m[j + 1]) << std::endl;
				open_val = closed_val + (dup_line.find(m[j + 1]) == -1 ? 0 : dup_line.find(m[j + 1]));
				mval.push_back(open_val);
				closed_val = open_val;
			}
		}
	}
	catch (std::exception& error) {
		std::cerr << "error : " << error.what() << std::endl;
	}

    for (auto mv : mval) std::cout << mv << ":";
    std::cout << std::endl;
}
Compiler stderr
<source>: In function 'int main()':
<source>:20:75: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::match_results<__gnu_cxx::__normal_iterator<const char*, std::__cxx11::basic_string<char> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   20 |                                                         for (int j = 1; j < m.size(); j++) {
      |                                                                         ~~^~~~~~~~~~
<source>:26:82: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   26 |                                                                 if (f == -1 || f > dup_line.size() - 1)
      |                                                                                ~~^~~~~~~~~~~~~~~~~~~~~
<source>:31:114: warning: comparison of integer expressions of different signedness: 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   31 |                                                                 open_val = closed_val + (dup_line.find(m[j + 1]) == -1 ? 0 : dup_line.find(m[j + 1]));
      |       
Program returned: 0
Program stdout
}
}
2
} } }
 } }
1
}
}
0
0:0:
Posted
Updated 19-Apr-23 17:04pm
v2
Comments
OriginalGriff 18-Apr-23 2:19am    
No. I for one am not following a link to a random website with gawd-know-what at the other end. Would you?

You want us to look at code, copy'n'paste the relevant fragments into your question, and your the "code block" option on the paste, or the "code" button on the toolbar to engage the syntax highlighter and preserve formatting.
Member 15981706 18-Apr-23 2:39am    
It seems like the regex you're using to search for "}" is wrong. It only searches the first and last character in a string whereas it skips the second character, thus you see only 2 matches are found.

Also, please be specific. I don't see any explanation like where you're facing this issue.

1 solution

should not line

C++
for (int j = 1; j < m.size(); j++)


be

C++
for (int j = 0; j < m.size(); j++)


?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900