Sign in to follow this  
Followers 0
FGFS

nested for -- regex

13 posts in this topic

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

}

0

Share this post


Link to post
Share on other sites

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

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.

0

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:
[tt]world
Hello, (str)! Expression: (4*2./20)[/tt]

And does not output:
[tt]world
Hello, world! Expression: 0.4[/tt]


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:
[tt]world
Hello, world! Expression: 0.4[/tt] Edited by fastcall22
1

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
1

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

0

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

0

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.

0

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.

0

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?

-1

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.

1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0