美文网首页
判断字符串是否符合ip:port模式

判断字符串是否符合ip:port模式

作者: 王晓宇_xiaoyuwang | 来源:发表于2019-03-16 23:58 被阅读0次

Linux下使用inet库

#include <arpa/inet.h>
#include <iostream>
#include <string>

using namespace std;

bool validate_ip_address(const char* ip, const bool& ipv6) {
  unsigned char buf[sizeof(struct in6_addr)];
  if (ipv6) { //ipv6
    if (inet_pton(AF_INET6, ip, buf))
      return true;
  }
  else { //ipv4
    if (inet_pton(AF_INET, ip, buf))
      return true;
  }
  return false;
}
bool validate_ip_address(const std::string& ip, bool ipv6) {
  return validate_ip_address(ip.data(), ipv6);
}

int main() {
    std::string s1("10.10.10.10:20");
    std::string s2("10.10.10.10");
    std::string s3("10.10.10:20");
    std::string s4("10.10.10.10:20.20");
    std::string s5("10.a10.10:20");
    std::string s6("10.10.10.a10:20");
    std::string s7("10");
    std::string s8("10.10.10.1001:30");
    std::string s9("10.10.10.1001:");
    std::string s10("10:1");
    std::string s11("10.10.10.10.10:10");
    std::string s12("10.10.10.10:a");
    std::string s13("10.10.10.10.10.10:101");
    std::string s14("300.10.10.10:20");
    std::string s15("0:0:0:0:0:FFFF:204.152.189.116");

    std::cout << s1 << " " << std::boolalpha << validate_ip_address(s1, false) << std::endl;
    std::cout << s2 << " " << std::boolalpha << validate_ip_address(s2, false) << std::endl;
    std::cout << s3 << " " << std::boolalpha << validate_ip_address(s3, false) << std::endl;
    std::cout << s4 << " " << std::boolalpha << validate_ip_address(s4, false) << std::endl;
    std::cout << s5 << " " << std::boolalpha << validate_ip_address(s5, false) << std::endl;
    std::cout << s6 << " " << std::boolalpha << validate_ip_address(s6, false) << std::endl;
    std::cout << s7 << " " << std::boolalpha << validate_ip_address(s7, false) << std::endl;
    std::cout << s8 << " " << std::boolalpha << validate_ip_address(s8, false) << std::endl;
    std::cout << s9 << " " << std::boolalpha << validate_ip_address(s9, false) << std::endl;
    std::cout << s10 << " " << std::boolalpha << validate_ip_address(s10, false) << std::endl;
    std::cout << s11 << " " << std::boolalpha << validate_ip_address(s11, false) << std::endl;
    std::cout << s12 << " " << std::boolalpha << validate_ip_address(s12, false) << std::endl;
    std::cout << s13 << " " << std::boolalpha << validate_ip_address(s13, true) << std::endl;
    std::cout << s14 << " " << std::boolalpha << validate_ip_address(s14, false) << std::endl;
    std::cout << s15 << " " << std::boolalpha << validate_ip_address(s15, true) << std::endl;
}
  1. 高版本GCC支持regex
  2. GCC 4.8不支持regex,手动实现
#include <iostream>
#include <string>
#include <cctype>
#include <cstdlib>

using namespace std;

bool validate_connection(std::string conn, bool ipv6)
{
    const char dot = '.';
    const char colon = ':';
    const size_t max_dots = ipv6 ? 5 : 3; //three dots in ipv4 and five dots in ipv6
    const size_t max_colons = 1; //one colon seperating ip and port
    const size_t max_digits = 3; //as most three digits in part of ip
    const int max_piece_val = 255; //255 is the max val of a piece of ip

    size_t dots = 0;
    size_t colons = 0;
    size_t port_len = 0;

    std::string::iterator iter = conn.begin();
    size_t piece_len = 0;
    int piece_val = 0;
    for (; iter != conn.end(); ++iter) {
        if (dots < max_dots) { //former three parts of ip
            if (*iter != dot) {
                if (!std::isdigit(*iter)) //only digit allowed
                    return false;
                if (piece_len == max_digits) //a part of ip contains no more than three digits
                    return false;
                if ((piece_val = piece_val * 10 + *iter - '0') > max_piece_val)
                    return false;
                ++piece_len;
            }
            else {
                ++dots;
                piece_val = 0;
                piece_len = 0;
            }
        }
        else { //the left is {4th part of ip}:port
            if (colons < max_colons) { //the forth part of ip
                if (*iter != colon) {
                    if (!std::isdigit(*iter))
                        return false;
                    if (piece_len == max_digits)
                        return false;
                    if ((piece_val = piece_val * 10 + *iter - '0') > max_piece_val)
                        return false;
                    ++piece_len;
                }
                else
                {
                    ++colons;
                    piece_len = 0;
                }
            }
            else { //port
                if (!std::isdigit(*iter))
                    return false;
                ++port_len;
            }
        }
    }

    if (dots < max_dots || colons < max_colons || port_len == 0)
        return false;

    return true;
}

int main()
{
    std::string s1("10.10.10.10:20");
    std::string s2("10.10.10.10");
    std::string s3("10.10.10:20");
    std::string s4("10.10.10.10:20.20");
    std::string s5("10.a10.10:20");
    std::string s6("10.10.10.a10:20");
    std::string s7("10");
    std::string s8("10.10.10.1001:30");
    std::string s9("10.10.10.1001:");
    std::string s10("10:1");
    std::string s11("10.10.10.10.10:10");
    std::string s12("10.10.10.10:a");
    std::string s13("10.10.10.10.10.10:101");
    std::string s14("300.10.10.10:20");

    std::cout << s1 << " " << std::boolalpha << validate_connection(s1, false) << std::endl;
    std::cout << s2 << " " << std::boolalpha << validate_connection(s2, false) << std::endl;
    std::cout << s3 << " " << std::boolalpha << validate_connection(s3, false) << std::endl;
    std::cout << s4 << " " << std::boolalpha << validate_connection(s4, false) << std::endl;
    std::cout << s5 << " " << std::boolalpha << validate_connection(s5, false) << std::endl;
    std::cout << s6 << " " << std::boolalpha << validate_connection(s6, false) << std::endl;
    std::cout << s7 << " " << std::boolalpha << validate_connection(s7, false) << std::endl;
    std::cout << s8 << " " << std::boolalpha << validate_connection(s8, false) << std::endl;
    std::cout << s9 << " " << std::boolalpha << validate_connection(s9, false) << std::endl;
    std::cout << s10 << " " << std::boolalpha << validate_connection(s10, false) << std::endl;
    std::cout << s11 << " " << std::boolalpha << validate_connection(s11, false) << std::endl;
    std::cout << s12 << " " << std::boolalpha << validate_connection(s12, false) << std::endl;
    std::cout << s13 << " " << std::boolalpha << validate_connection(s13, true) << std::endl;
    std::cout << s14 << " " << std::boolalpha << validate_connection(s14, false) << std::endl;
}

相关文章

  • 判断字符串是否符合ip:port模式

    Linux下使用inet库 高版本GCC支持regex GCC 4.8不支持regex,手动实现

  • 字符串面试题总结

    规则判断 判断字符串是否符合整数规则 判断字符串是否符合浮点数规则 判断字符串是否符合回文字符串规则 数字运算in...

  • 2018-10-23正则表达式

    正则表达式的语法 用处:1.判断某个字符串是否符合某个条件---判断输入的字符串是否是邮箱/手机号码,是否是ip地...

  • 校验字符串

    目录:1、校验字符串是否为空2、判断字符串是否符合邮政编码格式3、判断字符串是否全是整数4、判断字符串是否全部是字...

  • 正则表达式

    用来匹配符合某种规则的字符串 案例 检索字符串中符合某种规则多个子字符串 判断用户的输入是否符合某种规则 替换字符...

  • iOS正则表达式验证用户名、邮箱等信息是否合法

    判断字符串是否有空格 判断IP地址是否正确 判断密码格式是否正确 判断用户名格式是否正确(目前校验规则是大小写字符...

  • skynet socket C库代码分析

    connect 参数: 一个参数ip:port字符串,或者两个参数,分别是ip, port 返回值: 套接字内部i...

  • 百度大数据开发

    判断一个字符串是否合法 ip 引用传递和值传递

  • strings 字符串操作

    strings // 判断字符串前缀 // 判断字符串后缀 // 判断字符串是否包含子串 // 判断字符串s是否包...

  • Haskell 基本语法(二)模式匹配

    模式匹配包含一系列特定的模式,用来判断数据是否符合规则,且能够通过这些模式把符合要求的数据解构出来。Haskell...

网友评论

      本文标题:判断字符串是否符合ip:port模式

      本文链接:https://www.haomeiwen.com/subject/kbxymqtx.html