Extract IP address from a string using boost regex?

Floyd picture Floyd · Feb 22, 2011 · Viewed 10.3k times · Source

I was wondering if anyone can help me, I've been looking around for regex examples but I still can't get my head over it.

The strings look like this:

"User JaneDoe, IP: 12.34.56.78"

"User JohnDoe, IP: 34.56.78.90"

How would I go about to make an expression that matches the above strings?

Answer

Jerry Coffin picture Jerry Coffin · Feb 22, 2011

The question is how exactly do you want to match these, and what else do you want to exclude?

It's trivial (but rarely useful) to match any incoming string with a simple .*.

To match these more exactly (and add the possibility of extracting things like the user name and/or IP), you could use something like: "User ([^,]*), IP: (\\d{1,3}(\\.\\d{1,3}){3})". Depending on your input, this might still run into a problem with a name that includes a comma (e.g., "John James, Jr."). If you have to allow for that, it gets quite a bit uglier in a hurry.

Edit: Here's a bit of code to test/demonstrate the regex above. At the moment, this is using the C++0x regex class(es) -- to use Boost, you'll need to change the namespaces a bit (but I believe that should be about all).

#include <regex>
#include <iostream>

void show_match(std::string const &s, std::regex const &r) { 
    std::smatch match;
    if (std::regex_search(s, match, r))
        std::cout << "User Name: \"" << match[1] 
                  << "\", IP Address: \"" << match[2] << "\"\n";
    else
        std::cerr << s << "did not match\n";
}

int main(){ 

    std::string inputs[] = {
        std::string("User JaneDoe, IP: 12.34.56.78"),
        std::string("User JohnDoe, IP: 34.56.78.90")
    };

    std::regex pattern("User ([^,]*), IP: (\\d{1,3}(\\.\\d{1,3}){3})");

    for (int i=0; i<2; i++)
        show_match(inputs[i], pattern);
    return 0;
}

This prints out the user name and IP address, but in (barely) enough different format to make it clear that it's matching and printing out individual pieces, not just passing entire strings through.