Jump to content

  • Log In with Google      Sign In   
  • Create Account

Calling all IT Pros from Canada and Australia.. we need your help! Support our site by taking a quick sponsored surveyand win a chance at a $50 Amazon gift card. Click here to get started!


nested for -- regex


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
13 replies to this topic

#1 FGFS   Members   -  Reputation: 250

Like
0Likes
Like

Posted 16 September 2013 - 01:36 AM

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

}



Sponsor:

#2 BitMaster   Crossbones+   -  Reputation: 5818

Like
2Likes
Like

Posted 16 September 2013 - 01:51 AM

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.

#3 FGFS   Members   -  Reputation: 250

Like
0Likes
Like

Posted 16 September 2013 - 05:17 AM

Cannot figure it out. Any help? Thanks



#4 Bregma   Crossbones+   -  Reputation: 6358

Like
0Likes
Like

Posted 16 September 2013 - 08:09 AM

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

#5 fastcall22   Crossbones+   -  Reputation: 6248

Like
1Likes
Like

Posted 16 September 2013 - 10:01 AM

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, 16 September 2013 - 10:04 AM.

gzip: H4sIAAAAAAAEAG1QTUvEMBC991e8nvaiFfYoS7yo sLCo6MnjtJ1ugmkiyWRL/72z3T1YEQIJ8z4zA2Xp yPvt1qBpGrRFIJZkk9FyRyUzHCbKIHgn4hnZOrm1 TD0mG0HCCs+QGDGWziKXI6Wm2n++GYwUVH2mrGEE PnGCVQ8K8+JYfXA6URDEQfMZh5h6g5eoAlWJdeEI bbH2qYZf7XMUfw8f/Q0oMeZYNL9/WHF0uFEshvMr XYujd9SycFb+F18QcSOvlJauZ8ejqevdnV7/d550 e0t6prmunh73Bu+vz4c/XUeOQXfJgvKNkhf95U8/ Dtgmy5IBAAA=

#6 Bregma   Crossbones+   -  Reputation: 6358

Like
1Likes
Like

Posted 16 September 2013 - 12:33 PM

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, 16 September 2013 - 12:37 PM.

Stephen M. Webb
Professional Free Software Developer

#7 FGFS   Members   -  Reputation: 250

Like
0Likes
Like

Posted 17 September 2013 - 12:22 AM

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



#8 Washu   Senior Moderators   -  Reputation: 7004

Like
0Likes
Like

Posted 17 September 2013 - 01:20 AM

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.
ScapeCode - Blog | SlimDX


#9 FGFS   Members   -  Reputation: 250

Like
0Likes
Like

Posted 17 September 2013 - 09:38 AM

     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.



#10 FGFS   Members   -  Reputation: 250

Like
0Likes
Like

Posted 18 September 2013 - 04:00 AM

something alike:

 

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

 

is wrong. any ideas?



#11 Brother Bob   Moderators   -  Reputation: 9496

Like
0Likes
Like

Posted 18 September 2013 - 04:14 AM

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.



#12 FGFS   Members   -  Reputation: 250

Like
-1Likes
Like

Posted 18 September 2013 - 04:53 AM

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?



#13 Brother Bob   Moderators   -  Reputation: 9496

Like
1Likes
Like

Posted 18 September 2013 - 04:58 AM

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.



#14 FGFS   Members   -  Reputation: 250

Like
0Likes
Like

Posted 18 September 2013 - 10:56 PM

Aah ok got it, thanks.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS