Regular Expression to match IP address + wildcard

后端 未结 5 874
旧时难觅i
旧时难觅i 2021-01-02 21:57

I\'m trying to use a RegularexpressionValidator to match an IP address (with possible wildcards) for an IP filtering system.

I\'m using the following Regex:

5条回答
  •  借酒劲吻你
    2021-01-02 22:31

    My answer is general for .NET, not RegularExpressionValidator-specific.

    Regex string for IP matching (use ExplicitCapture to avoid useless capturing and keep RE concise):

    "\\b0*(2(5[0-5]|[0-4]\\d)|1?\\d{1,2})(\\.0*(2(5[0-5]|[0-4]\\d)|1?\\d{1,2})){3}\\b"
    

    Depending on particular use case you may want to add appropriate anchors, i.e. \A or ^ at the beginning and \Z or $ at the end. Then you can remove word-boundaries requirement: \b.

    (Remember about doubling \ inside the string)

提交回复
热议问题