Ubuntu Forums ubuntu.com - launchpad.net - ubuntu help  

Go Back   Ubuntu Forums > The Ubuntu Forum Community > Other Community Discussions > Development & Programming > Programming Talk
Register Reset Password Forum Help Forum Council Search Today's Posts Mark Forums Read

Ubuntu 9.10 is out!!!

When downloading Ubuntu 9.10 please consider using bittorrent to get your copy of Ubuntu.

The Ubuntu Developers Summit for Lucid Lynx will be held the week of 16-Nov-2009 till 20-Nov-2009 in Dallas, TX USA. Visit the the Ubuntu wiki for more information about UDS and how to participate remotely.

Programming Talk
This forum is for all programming questions.
The questions do not have to be directly related to Ubuntu and any programming language is allowed.

 
Thread Tools Display Modes
Old September 24th, 2008   #1
StOoZ
Has an Ubuntu Drip
 
Join Date: Feb 2008
Beans: 772
Ubuntu 9.10 Karmic Koala
[SOLVED] anyone familiar with boost regex?

im trying to learn it , now I have a string , which has a LOT of jpg's mentioned in it...
I wonder why this code , returns only one , it should match all of them (isnt it?):
http://rafb.net/p/IOGJO849.html
StOoZ is offline   Reply With Quote
Old September 24th, 2008   #2
dwhitney67
Ubuntu addict and loving it
 
dwhitney67's Avatar
 
Join Date: Jun 2007
Location: Maryland, US
Beans: 3,562
Ubuntu 9.10 Karmic Koala
Re: anyone familiar with boost regex?

Quote:
Originally Posted by StOoZ View Post
im trying to learn it , now I have a string , which has a LOT of jpg's mentioned in it...
I wonder why this code , returns only one , it should match all of them (isnt it?):
http://rafb.net/p/IOGJO849.html
There are many utilities in Boost that look wonderful on paper, but when you actually try to implement them, you realize that you could have solved the problem much easier if you implemented (and documented) your code appropriately.

Now... what is it that you are trying to do? What does you data file contain? Just strings with the .jpg extension?

If you are bent on using Boost, will this example not help you?
http://www.boost.org/doc/libs/1_35_0...or_example.cpp

P.S. I would prefer if you post your code directly onto the Ubuntu Forums, without referring to another site.
dwhitney67 is offline   Reply With Quote
Old September 26th, 2008   #3
StOoZ
Has an Ubuntu Drip
 
Join Date: Feb 2008
Beans: 772
Ubuntu 9.10 Karmic Koala
Re: anyone familiar with boost regex?

mmm ok , what im trying to do , to be more precise , is , I have something like this , a file with many links , could be https , http , etc , all of them are jpg links only , now I would like to keep all these links in a vector or set , the file looks something like this (i manually wrote that one , the other can have anything in it , but it also has links , like this one has) :
Quote:
hhh....fdsfds jpj<>http://www.gg.com/h.jpg<<>fdsfs
hhh....fdsfds jpj<>http://www.gg.com/h.jpg<<>fdsfs
sdfsfsdsd
hhhsd....fdsfds jpj<>http://www.gg.com/h.jpg<<>fdsfs
sf...sdfsfsdfdsfds jpj<>http://www.gg.com/h.jpg<<>fdsfs
sdfsdfsdfsdf
sfsf
hhh.f
hhhsf.fsd...fdsfdsfds jpj<>http://www.gg.com/h.jpg<<>fdsfs


hhh....fdsfds jfdfdsfdspj<>http://www.ggdsssssss.com/h.jpg<<>fdsfs
hhh....fdsfds jpj<>http://www.gg.com/h.jpg<<>fdsfs
hhfsdfsdh....fdsfds jpj<>http://www.fsfsdfsfsdgg.com/fffh.jpg<<>fdsfs
hhh....fdsfds jpj<>http://www.gg.com/h.jpg<<>fdsfs
sdfsdfdshfh.sdf...fdsfds jpj<>http://www.gddsdsdsdsg.com/h.jpg<<>fdsfs
hhhds....fdsfds jpj<>http://www.gdfgfddg.com/h.jpg<<>fdsfs
hhhdf....fdsfds jpj<>http://www.gg.com/h.jpg<<>fdsfs
hhhf....fdsfds jpj<>http://www.gg.com/h.jpg<<>fdsfs
hhhsd....fdsfds jpj<>http://www.ggfff.com/h.jpg<<>fdsfs
hhsdfh....fdsfds jpj<>http://www.gg.com/h.jpg<<>fdsfs
hfhh....fdsfds jpj<>http://www.gg.com/h.jpg<<>fdsfs
hhh...sdf.fdsfds jpj<>http://www.gg.com/h.jpg<<>fdsfs
hhh...sd.fdsfds jpj<>http://www.gg.com/uuuuh.jpg<<>fdsfs
hhhfds...sfsddfdsfds jpj<>http://www.gAAAAAAADDDg.com/h.jpg<<>fdsfs
hhh..fsd..fdfsdsfds jpj<>http://www.gg.com/iuiuh.jpg<<>fdsfs
vv hhh....fdsfds jpj<>http://www.gg.com/hiuiu.jpg<<>fdsfs
hhh....fdsfds jpj<>http://www.gg.com/iuh.jpg<<>fdsfs
so basically I want to grab all of these links...
StOoZ is offline   Reply With Quote
Old September 26th, 2008   #4
Zugzwang
Dark Roasted Ubuntu
 
Zugzwang's Avatar
 
Join Date: Oct 2007
Beans: 1,026
Ubuntu 8.10 Intrepid Ibex
Re: anyone familiar with boost regex?

RegExes seem to be overkill here. Just split your input strings on the characters '>' and '<' and apply a check for each of the strings you get. See here, section 7.3 for such a tokenizing example.
Zugzwang is offline   Reply With Quote
Old September 26th, 2008   #5
dwhitney67
Ubuntu addict and loving it
 
dwhitney67's Avatar
 
Join Date: Jun 2007
Location: Maryland, US
Beans: 3,562
Ubuntu 9.10 Karmic Koala
Re: anyone familiar with boost regex?

Quote:
Originally Posted by Zugzwang View Post
RegExes seem to be overkill here. Just split your input strings on the characters '>' and '<' and apply a check for each of the strings you get. See here, section 7.3 for such a tokenizing example.
+1

Now that the data file has been presented, I agree that it would be rather easy to yank a sub-string from each line within the file.

If the OP is still interested in a RegEx solution using Boost, here's something I threw together this morning:


PHP Code:
#include <boost/regex.hpp>

#include <string>
#include <vector>
#include <fstream>
#include <iostream>


class HttpRegex
{
  public:
    
typedef std::vectorstd::string Results;

    
HttpRegex( const char *regExpression )
      : 
m_re(regExpression)
    {
    }

    
Results findResultsstd::ifstreamifs )
    {
      
Results results;

      if ( !
ifs.bad() )
      {
        
// Setup regular expression
        
boost::regex expressionm_re );

        
std::string line;

        
// Read each line of text from the file
        
while ( getlineifsline ) )
        {
          
// Find the desired expression
          
boost::sregex_iterator m1line.begin(), line.end(), expression );
          
boost::sregex_iterator m2;

          
// Save the result
          
while ( m1 != m2 )
          {
            
// thru trial/error I determined where the URL is at.
            
results.push_back( (*m1)[2] );
            ++
m1;
          }
        }
      }

      return 
results;
    }

  private:
    const 
char m_re;
};


int mainint argc, const char** argv )
{
  if ( 
argc != )
  {
    
std::cout << "Usage: " << argv[0] << " <file>" << std::endl;
    return 
1;
  }

  
// Open the file
  
std::ifstream ifsargv[1] );

  
// Get HTTP results
  
HttpRegex          regex"(.*)<>(.*)<<>" );
  
HttpRegex::Results results regex.findResultsifs );

  
// Close the file (if that was not already obvious)
  
ifs.close();

  
// Display results
  
std::cout << results.size() << " matches found." << std::endl;

  for ( 
HttpRegex::Results::const_iterator it results.begin(); it != results.end(); ++it )
  {
    
std::cout << *it << std::endl;
  }

  return 
0;


P.S. To compile:
Code:
g++ regex.cpp -lboost_regex -o regex

Last edited by dwhitney67; September 26th, 2008 at 08:56 PM..
dwhitney67 is offline   Reply With Quote

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 02:10 AM.


vBulletin ©2000 - 2009, Jelsoft Enterprises Ltd. Ubuntu Logo, Ubuntu and Canonical © Canonical Ltd. Tango Icons © Tango Desktop Project. lingonberry