nested for -- regex

Started by
12 comments, last by FGFS 10 years, 7 months ago

Hi

Little confused. How to most simple output all matches. For ex. .m_airport_map contains LSZA, LSZB, LSZH and many more. Now I want to

show by giving L as name all starting with L, giving LS all starting with LS etc.

Many thanks

for (auto it = test.m_airport_map.begin(); it != test.m_airport_map.end(); it++)
{

auto mapEntry = (*it);
// std::cout << mapEntry.first << std::endl;

std::smatch m;
std::regex e ("(name.c_str())(.*)");

std::regex_match ( mapEntry.first, m, e );

std::cout << "matches:" << std::endl;
for (std::smatch::iterator it = m.begin(); it!=m.end(); ++it) {
std::cout << *it << std::endl;
}

}

Advertisement

std::regex e ("(name.c_str())(.*)");


This almost certainly does not do what you intend it to do and having no matches at all will probably be the correct result.

Cannot figure it out. Any help? Thanks

Are you sure you need to use regex here? Your description sounds like std::string::starts_with() would be simpler and more appropriate.

Stephen M. Webb
Professional Free Software Developer

Cannot figure it out. Any help? Thanks

#include <string>
#include <iostream>

using namespace std;

int main() {
    string str = "world";
    string expr = "Hello, (str)! Expression: (4*2./20)";

    cout << str << endl;
    cout << expr << endl;
}
Outputs:
world
Hello, (str)! Expression: (4*2./20)


And does not output:
world
Hello, world! Expression: 0.4



On the other hand, the following code:
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int main() {
    string str = "world";
    ostringstream temp;
    temp << "Hello, " << str << "! Expression: " << (4*2./20);
    string expr = temp.str();

    cout << str << endl;
    cout << expr << endl;
}
... does output:
world
Hello, world! Expression: 0.4
Here's a brief, complete program that uses regex to match output all names beginning with "LS" from a vector of candidate names. Perhaps it will help.

#include <iostream>
#include <regex>
#include <string>
#include <vector>

std::vector<std::string> airport_map = {
  "LSZA", "LSZB", "LSZH", "LZBB", "OMFG"
};


int
main()
{
  std::regex re("^LS");
  for (auto const& name: airport_map)
  {
    if (std::regex_search(name, re))
    {
      std::cout << name << "\n";
    }
  }
}

Stephen M. Webb
Professional Free Software Developer

Thanks Bregma but even debugging through I saw LS* airports for mapEntry.first in debugger which were not found.

vector<Flughafen*> loopAirportpush(const string &name)
{

std::regex re("^LS"); // I want to use name here
for (auto it = test.m_airport_map.begin(); it != test.m_airport_map.end(); it++)
{

auto mapEntry = (*it);

if (std::regex_search(mapEntry.first, re))
{
std::cout << mapEntry.first << "\n";
}

This looks like a homework assignment, also moved to for-beginners (as it seems to be an issue with the understanding of the basic language).

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

std::regex e ("(0)(.*)");

the above seems to work if 0 is the first char. Now I need to integrate the string name. Well I have a look tomorrow if nobody is faster.

something alike:

std::regex e ("("name.c_str()")(.*)");

is wrong. any ideas?

This topic is closed to new replies.

Advertisement