C++ and Boost Regex

Well, this week I have finally completed some tasks, although it’s a slow progress, but I’m satisfied with myself. FYI, I had numerous encounters with pointer and addresses. In one of my tasks, I had to assign preset addresses to an object. Usually, once we declare a variable the address of the variable is automatically set. What if we want to set an address to a variable? A beginner as I am, I actually thought the correct way to do this was&foo= 0x8000 Running this code simply gives the error lvalue required as left operand of assignment After some pointer and addresses tutorials by my colleague Peter, I was again enlightened. When we declare a pointer, for example : int *foo;, foo is actually an address! So after initializing the pointer int *foo;, the correct thing to do is simply foo=0x8000. Then again, you will encounter an error An invalid conversion from int to int* The solution is to cast the address to an int pointer type. After numerous attempts in debugging, I feel that I have grown much fonder of pointers and addresses.

I was also tasked to edit a generated text file by adding in a few parameters of my own. I took some time to understand how the file was generated at first. By using readelf command in system(), certain user entered details were saved in a separate text file. I understood how Boost::Regex played an important role in data extraction. For those who don’t know about boost::regex, i strongly suggest you to try out the examples at http://regexone.com/ With the correct expressions, any data can be extracted and “catched”. I will explain a few simple steps to start using regex in C++.

std::string String;
boost::regex re{“abc(ef)”};
boost::cmatch matches;

boost::regex_match(String, matches, re);

“String” is the string to be parsed. The regular expression used here is a simple one. A “()” is used in regex to catch matches. So basically, passing a string “abcefgh” will return a true with regex_match. An array of matches will be created based on the number of “catches”. matches[0] would be default the whole string and matches[1] will be “ef”.

I must say I really enjoy the work here, where we must learn to work independently and put our knowledge into real application. We had a movie “day” this week too, where Dr. Shawn treated all of us to “Scorch Trials”, during work time! Although he can be strict sometimes during work, at times, he can be a cool boss.

You may also like...

Leave a Reply