Boost.Regex Search - help for newbie

G

Giuseppe

Hi,
i have installed yesterday the boost library on my linux kubuntu; but
the documentation is not too useful to me.

What i need is to search for a five digit number in a string like
this:

<td><img src="/images/code.ashx?x=33717" id="codeimg" width="204"
height="40" alt="" /></td>

(33717 in this case) and save the match in a int ( or also a string,
doesn't matter). The regular expression to use should be "[\d]
{5,5}" ( or "[\\d]{5,5}" in my c++ code ) but i can't accomplish this.

Can somebody post a simple main() showing how to do?

Thanks,
Giuseppe
 
T

tragomaskhalos

(33717 in this case) and save the match in a int ( or also a string,
doesn't matter). The regular expression to use should be "[\d]
{5,5}" ( or "[\\d]{5,5}" in my c++ code ) but i can't accomplish this.

Not au fait with boost Regex, but to capture a match in a regex you
typically need to surround it with parentheses;
so try "([\\d]{5})".
HTH
 
P

Pete Becker

Giuseppe said:
(33717 in this case) and save the match in a int ( or also a string,
doesn't matter). The regular expression to use should be "[\d]
{5,5}" ( or "[\\d]{5,5}" in my c++ code ) but i can't accomplish this.

There are several ways to write a regular expression that matches 5
digits. Here are a few:

[[:digit:]]{5,5}
\d\d\d\d\d
[[:digit:]]{5,5}
\d{5,5}

You don't need the square brackets when you use \d, because \d matches
any digit character.

To write those regular expressions as C== strings, double the backslashes:

"\\d\\d\\d\\d\\d"
"[[:digit:]]{5,5}"
"\\d{5,5}"

And, as has been pointed out, put parentheses around whatever you want
to capture:

"(\\d\\d\\d\\d\\d)"
"([[:digit:]]{5,5})"
"(\\d{5,5})"


Now put that in a string object:

std::string str("(\\d{5,5}");

Now (using TR1's regex, based on Boost's) create a regular expression
object:

std::tr1::regex rgx(str);

Next, create a match object to hold the result of the search:

std::tr1::cmatch match;

Now do the search, and display the result:

if (std::tr1::regex_match(target, match, rgx))
std::cout << match[1] << '\n';

For more details, see chapters 14-21 of my book, "The C++ Standard
Library Extensions."

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
 
P

Pete Becker

Pete Becker wrote:

Slow start this morning. Several minor edits:
There are several ways to write a regular expression that matches 5
digits. Here are a few:

[[:digit:]]{5,5}
\d\d\d\d\d
[[:digit:]]{5,5}
\d{5,5}

The first and third are the same. Ignore either one of them.
You don't need the square brackets when you use \d, because \d matches
any digit character.

To write those regular expressions as C== strings, double the backslashes:

Yup, that's the new languag, C==. But in this case, you can think of it
as C++.
"\\d\\d\\d\\d\\d"
"[[:digit:]]{5,5}"
"\\d{5,5}"

And, as has been pointed out, put parentheses around whatever you want
to capture:

"(\\d\\d\\d\\d\\d)"
"([[:digit:]]{5,5})"
"(\\d{5,5})"

Actually, for this match you don't need the parentheses. You could use
match[0] in the final line. But in general, if what you're looking for
isn't the entire match, you need capture groups for whatever interests you.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
 
D

David Harmon

On 7 Feb 2007 04:32:29 -0800 in comp.lang.c++, "tragomaskhalos"
Not au fait with boost Regex, but to capture a match in a regex you
typically need to surround it with parentheses;
so try "([\\d]{5})".

Of course that will match a larger number too, which
the OP's {5,5} suggests is not wanted. Perhaps:

#include <iostream>
#include <string>
using namespace std;
#include <boost/regex.hpp> // <-- http://www.boost.org
int main()
{
string line;
boost::regex rexp("\\D(\\d{5,5})\\D");
boost::smatch tokens;
while (getline(cin, line)) {
if (boost::regex_search(line, tokens, rexp)) {
cout << tokens.str(1) << " => " << line << '\n';
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top