Using boost's reg.expression

Hi everyone! I'm having troubles when using the regular expression tools developed by boost libraries.

My specific problem is: how can I print out January, just adding some lines to the following code?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <string>

#include <boost/regex.hpp>

int
main()
{

 boost::regex my_regex("\\b(jan)", boost::regex_constants::icase);

 std::string my_month = "It's January!!";

 std::cout << my_month << std::endl;

 if (boost::regex_search(my_month, my_regex,
                        boost::regex_constants::format_perl))
  {
   std::cout << "boost::regex_search() returned 1 \n";
  }

 else
  {
   std::cout << "boost::regex_search() returned 0 \n";
  }

 return 0;
}


Thank you in advance,

Giuseppe
You could simply add "cout << "January"" between lines 18 and 20.

If you're looking to capture the entire word that begins with (case-insensitively) "jan", you would have to modify the regex:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
#include <boost/regex.hpp>

int main() {
    boost::regex my_regex("\\b(jan\\w*)", boost::regex::icase);
    std::string my_month = "It's January!!";
    std::cout << my_month << '\n';

    boost::smatch m;
    if (boost::regex_search(my_month, m, my_regex))
        std::cout << "boost::regex_search() returned true and matched '"
                  << m[0] << "'\n";
    else 
        std::cout << "boost::regex_search() returned false \n";
}


demo: http://liveworkspace.org/code/28FT1u$0
Ok, thank you. I've understood something more about this boost's regex. But, I'm far from my real goal, yet. Say, I have some kind of strange string that contains a bithdate. For instance, say that I want to extract the date 21 June 1992 from !!eh?21**?junAn1992.

How can I solve this problem? Reading Boost's documentation it sounds very weard that one wants to extract data from a string and not just knowing if the regex matches.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <string>
#include <boost/regex.hpp>

int main()
{

 // Let's start from printing out just the 21.
 // So the regex I've thought is the following one:
 boost::regex my_reg_expression ("[^\\d]*\\d{1,2}",
                                 boost::regex_constants::icase);

 // Here's the strange string:
 std::string my_string = "!!eh21**?junAn1992pl6=";
 
 
 boost::smatch m;
 
 if (boost::regex_search(my_string, m, my_reg_expression,
                     boost::regex_constants::format_perl))
 {                    
  std::cerr << "m.size() returns " << m.size() << std::endl;
  std::cerr << "m.str(0) == " << m.str(0) << std::endl;
 }
 
 else
 {
  std::cerr << "Sorry but regex has not matched. \n";
 }
  
 return 0;
}


Exit code:
1
2
m.size() returns 1
m.str(0) == !!eh21


Can someone help me?

Thank you in advance.
it's really the same as any other regular expression. Yours matches everything up to the second digit, and that's what you get. You could use round parentheses to make the digits a sub expression, same as in the first example. Could you describe your "real goal" in more detail?
Topic archived. No new replies allowed.