• Advertisement
Sign in to follow this  

nested for -- regex

This topic is 1587 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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;
            }

}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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
Edited by fastcall22

Share this post


Link to post
Share on other sites
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";
    }
  }
}
Edited by Bregma

Share this post


Link to post
Share on other sites

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";
              }

Share this post


Link to post
Share on other sites

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).

Share this post


Link to post
Share on other sites

     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.

Share this post


Link to post
Share on other sites

something alike:

 

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

 

is wrong. any ideas?

Share this post


Link to post
Share on other sites

something alike:

 

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

 

is wrong. any ideas?

fastcall22 has addressed that issue above already, with example code of what is wrong and how to make it work.

Share this post


Link to post
Share on other sites

Again, I want to list all beginning with the string "name".

Something alike:

    std::regex e ("^"name);

 

is wrong. Moderators, if it's that easy, why don't you give the correct code and not some unusable handicrafts?

Share this post


Link to post
Share on other sites

But you have been given the correct solution already; see fastcall22's post above. If you don't understand it, then ask for clarification and we may answer to help you learn why and how it works, but spoon feeding a solution is not helping anyone.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement