C++ Regular Expressions with Boost Regex

前端 未结 3 1321
忘掉有多难
忘掉有多难 2020-12-05 12:36

I am trying to take a string in C++ and find all IP addresses contained inside, and put them into a new vector string.

I\'ve read a lot of documentation on regex, b

3条回答
  •  时光说笑
    2020-12-05 13:01

    The offered solution is quite good, thanks for it. Though I found a slight mistake in the pattern itself.

    For example, something like 49.000.00.01 would be taken as a valid IPv4 address and from my understanding, it shouldn't be (just happened to me during some dump processing).

    I suggest to improve the patter into:

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

    This should allow only 0.0.0.0 as the all-zero-in, which I suppose to be correct and it will eliminate all .00. .000. etc.

提交回复
热议问题