I'm sorry for my very late reply!
Thank you Duthomhas and ne555 for your responses! Reading them both + doing some searching online allowed me to solve the issue. :)
However, I just realized that I also need to add support for integers and doubles (outside of quotes). I tried adding that in the std::regex desiredFormat line but it gives errors. Here is my current code (minus some notes):
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
|
std::string line;
//std::regex desiredFormat(R"("[^"]*")"); // Original line when I was only detecting "contents. like this!" and not ints or doubles outside of quotes.
// Attempted Solution 1:
std::regex r1(R"("[^"]*")");
std::regex r2(R"([0-9]*.[0-9]*)");
std::regex r3("[0-9]*");
//std::regex desiredFormat(r1|r2|r3);
// Attempted Solution 2:
std::regex desiredFormat(R"("[^"]*")" | R"([0-9]*.[0-9]*)" | "[0-9]*");
// Attempted Solution 3:
//std::regex desiredFormat((R"("[^"]*")") (R"([0-9]*.[0-9]*)") ("[0-9]*"));
while (std::getline(inFile, line)) {
std::cout << "[Debug] Line = \t" << line << "\n";
// Setup Regex Iterator:
auto lineBegin = std::sregex_iterator(line.begin(), line.end(), desiredFormat);
auto lineEnd = std::sregex_iterator();
// Iterate through line using regex_search():
for (std::sregex_iterator iter = lineBegin; iter != lineEnd; ++iter) {
std::smatch match = *iter;
std::string matchStr = match.str();
std::cout << matchStr << "\n";
alphaNums.push_back(matchStr);
}
}
// 5.
// Closes File:
inFile.close();
}
| |
Error:
$ g++ -std=c++17 main3.cpp -lstdc++fs -Wall -Wextra -Wshadow -Wnon-virtual-dtor -pedantic
main3.cpp: In function ‘void parseJsonFile(const std::filesystem::__cxx11::path&, std::vector<std::__cxx11::basic_string<char> >&)’:
main3.cpp:269:47: error: invalid operands of types ‘const char [8]’ and ‘const char [14]’ to binary ‘operator|’
269 | std::regex desiredFormat(R"("[^"]*")" | R"([0-9]*.[0-9]*)" | "[0-9]*");
| ~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~~~~
| | |
| const char [8] const char [14] |
Note: If absolutely necessary you may view my full code here but I should warn you it's probably a mess and comments may not be up-to-date:
https://pastebin.com/iFZeY5MS
Help would be greatly appreciated. Thanks!
Edit: Also, the solution I'm trying to do I found from here -
https://stackoverflow.com/a/21983196
And another source I found very helpful was:
https://www.regular-expressions.info/dot.html
Edit 2:
ne555 wrote: |
---|
> I would use std::regex_replace() for this.
it's overkill to use regex for that task
`string::find()' and `string::rfind()' should suffice |
Thanks for the suggestion! Although I couldn't figure out how to get it to work with any contents inside the quote, without using regex. Some examples:
Line 1:
"Item" this "item:id"
---->
"Item"
and
"item:id"
should be stored in a vec.
Line 2:
"Hello World" apple heels seeds "this"
---->
Hello World"
and
"this"
should be stored in a vec.
Line 3:
"Cow 32.3"
---->
"Cow 32.3"
should be stored in a vec.