确定字符串是否包含IPv4地址的好方法是什么?我应该用isdigit()
吗?
我问过类似的C++问题.您应该能够使用我当时提出的稍微修改过的(对于C)版本.
bool isValidIpAddress(char *ipAddress) { struct sockaddr_in sa; int result = inet_pton(AF_INET, ipAddress, &(sa.sin_addr)); return result != 0; }
您需要#include
使用inet_pton()函数.
根据对问题的评论进行更新:如果您想知道C风格的字符串是否包含 IP地址,那么您应该结合到目前为止给出的两个答案.使用正则表达式查找与IP地址大致匹配的模式,然后使用上面的函数检查匹配项以查看它是否真实.
这是我不久前写的一个嵌入式系统的例程,它在网络上生成了各种可疑模式.因此,它绝对不使用像网络库甚至标准C库这样的花哨的东西,而是更喜欢避免所有现代的东西,如字符串标记和(颤抖)正则表达式库:-)为此,它适合于你可以找到自己的任何环境,而且速度非常快.
虽然,如果你在一个类似的环境中checkIp4Addess()
,我建议你使用它.这表明你在做嵌入式工作时有时需要忍受的东西(尽管这是一个真正的解决方案).
int isValidIp4 (char *str) { int segs = 0; /* Segment count. */ int chcnt = 0; /* Character count within segment. */ int accum = 0; /* Accumulator for segment. */ /* Catch NULL pointer. */ if (str == NULL) return 0; /* Process every character in string. */ while (*str != '\0') { /* Segment changeover. */ if (*str == '.') { /* Must have some digits in segment. */ if (chcnt == 0) return 0; /* Limit number of segments. */ if (++segs == 4) return 0; /* Reset segment values and restart loop. */ chcnt = accum = 0; str++; continue; } /* Check numeric. */ if ((*str < '0') || (*str > '9')) return 0; /* Accumulate and check segment. */ if ((accum = accum * 10 + *str - '0') > 255) return 0; /* Advance other segment specific stuff and continue loop. */ chcnt++; str++; } /* Check enough segments and enough characters in last segment. */ if (segs != 3) return 0; if (chcnt == 0) return 0; /* Address okay. */ return 1; }
int isValidIp4 (char *str) { int segs = 0; /* Segment count. */ int chcnt = 0; /* Character count within segment. */ int accum = 0; /* Accumulator for segment. */ /* Catch NULL pointer. */ if (str == NULL) return 0; /* Process every character in string. */ while (*str != '\0') { /* Segment changeover. */ if (*str == '.') { /* Must have some digits in segment. */ if (chcnt == 0) return 0; /* Limit number of segments. */ if (++segs == 4) return 0; /* Reset segment values and restart loop. */ chcnt = accum = 0; str++; continue; } /* Check numeric. */ if ((*str < '0') || (*str > '9')) return 0; /* Accumulate and check segment. */ if ((accum = accum * 10 + *str - '0') > 255) return 0; /* Advance other segment specific stuff and continue loop. */ chcnt++; str++; } /* Check enough segments and enough characters in last segment. */ if (segs != 3) return 0; if (chcnt == 0) return 0; /* Address okay. */ return 1; }