Jump to content

  • Log In with Google      Sign In   
  • Create Account

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   Members   

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

}



#2   Members   

8647
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   Members   

345
Like
0Likes
Like

Posted 16 September 2013 - 05:17 AM

Cannot figure it out. Any help? Thanks



#4   Members   

8321
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   Moderators   

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

zlib: eJzVVLsSAiEQ6/1qCwoK i7PxA/2S2zMOZljYB1TO ZG7OhUtiduH9egZQCJH9 KcJyo4Wq9t0/RXkKmjx+ cgU4FIMWHhKCU+o/Nx2R LEPgQWLtnfcErbiEl0u4 0UrMghhZewgYcptoEF42 YMj+Z1kg+bVvqxhyo17h nUf+h4b2W4bR4XO01TJ7 qFNzA7jjbxyL71Avh6Tv odnFk4hnxxAf4w6496Kd OgH7/RxC

#6   Members   

8321
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   Members   

345
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   Senior Moderators   

7714
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   Members   

345
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   Members   

345
Like
0Likes
Like

Posted 18 September 2013 - 04:00 AM

something alike:

 

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

 

is wrong. any ideas?



#11   Moderators   

10109
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   Members   

345
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   Moderators   

10109
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   Members   

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