Jump to content

  • Log In with Google      Sign In   
  • Create Account

C++11 regex training


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
2 replies to this topic

#1 Khatharr   Crossbones+   -  Reputation: 3083

Like
1Likes
Like

Posted 07 June 2013 - 09:47 PM

Hello all.

 

I've used Perl-style regular expressions extensively in Ruby, but previously I've done all my C-ish string parsing manually. Now that I'm more familiar with STL and I have a compiler that actually supports C++11 I've immediately run across an opportunity to employ this new tool: I want to parse an HTTP header and extract two specific pieces of information.

 

So, I googled around a little bit, looking for a simple, straightforward explanation of how std::regex is used, but the examples and tutorials I found were, uh...

 

Basically what I want to do is this:

 

#Ruby style
length = -1
if header =~ /Content-Length: (.*?)$/
  length = $1.to_i
end

I don't really need a direct answer to how to do that with <regex>, but I'd really like to get my hands on a tutorial that does more than show how to check if user input is an integer or not.

 

Does anyone know of a good walkthrough for this library that covers common uses like capture groups or gsub'bing?

 

Thanks in advance for any advice.

 

------------------------------------------------

 

Murphy's Law of forum posting:

 

I found a series that walks through it immediately after posting. I don't know what the hell my problem is. Sorry.

 

http://solarianprogrammer.com/2011/10/12/cpp-11-regex-tutorial/


void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

Sponsor:

#2 Matt-D   Crossbones+   -  Reputation: 1469

Like
0Likes
Like

Posted 08 June 2013 - 12:59 PM

Two good tutorials:

 

C9 Lectures: Stephan T. Lavavej - Standard Template Library (STL), Part 8: Regular Expressions
https://channel9.msdn.com/Series/C9-Lectures-Stephan-T-Lavavej-Standard-Template-Library-STL-/C9-Lectures-Stephan-T-Lavavej-Standard-Template-Library-STL-8-of-n

Stephan T. Lavavej: Regex in C++11 and Boost @ C++Now! 2012

Slides: http://sdrv.ms/14mRyNH
 



#3 Khatharr   Crossbones+   -  Reputation: 3083

Like
0Likes
Like

Posted 08 June 2013 - 09:03 PM

Thanks, man. The C++11 style solution to my Ruby snippet there is as follows:

 

//'buffer' is a char array that's loaded from the socket with the first 1024 bytes of the repsonse
  std::string resp(buffer);
  std::regex rxLen("Content-Length: ([[:digit:]]+)");
  std::smatch lengthMatches;
  
  int length = 0;
  if(std::regex_search(resp, lengthMatches, rxLen)) {
    std::stringstream to_int;
    to_int << lengthMatches[1].str(); //lengthMatches[0] contains the full match. Subsequent indexes are capture group matches.
    to_int >> length;
  }

//If length is zero then the match failed, otherwise length will contain the file length indicated by the header.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.




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