开发者

C++ Regular Expressions

开发者 https://www.devze.com 2023-02-28 19:15 出处:网络
I\'m new to regular expressi开发者_运维百科ons - I am using Poco in C++ to pull IP addresses from a string and (trying) to put all IP matches into a string vector:

I'm new to regular expressi开发者_运维百科ons - I am using Poco in C++ to pull IP addresses from a string and (trying) to put all IP matches into a string vector:

string result = tcpClient.receiveRaw();

RegularExpression re("\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b");

RegularExpression::MatchVec matches;
re.match(result, 0, matches);

IPlist = result.substr(matches[1].offset, matches[1].length);

I don't think the last line is correct (not sure how it works) I'm trying to pull all IP addresses from that initial string and add them to a string vector.


The expression can be shortened and the \b probably needs another escape (but I am not familiar with Poco so I could be way off). Try this out:

RegularExpression re("\\b((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b");


Why not just use the C POSIX regular expressions in ? Here's the documentation:

http://pubs.opengroup.org/onlinepubs/007908799/xsh/regcomp.html

Beyond that, what's actually wrong here? You didn't specify an actual problem. I'm not familiar with the class you're using but nothing looks obviously broken, except that we don't know what IPlist is. It appears to be a list but is being set to equal a single string value. I suspect the logic there may be undesirable.

0

精彩评论

暂无评论...
验证码 换一张
取 消