You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何编写匹配带前缀IPv6的正则?现有正则适配问题修复

How to Write a Regex for IPv6 Addresses with Prefixes (and Fix Your Existing One)

Hey there! Let's break down how to craft a regex that matches IPv6 addresses with prefixes, and fix your existing regex that's currently missing this case.

First, Let's Clarify the Format

An IPv6 address with a prefix follows this structure: [IPv6 Address]/[Prefix Length]. Here's what each part means:

  • The IPv6 address itself can be full, compressed (using :: for consecutive zero blocks), or even include a zone identifier (like fe80::1%eth0 for link-local addresses tied to a network interface).
  • The prefix length is an integer between 0 and 128 (inclusive)—no more, no less.

Step 1: Start with a Solid Base IPv6 Regex

First, you need a regex that correctly matches all valid IPv6 address variants (since your existing one already handles some cases, this might align with what you have). Here's a robust base pattern:

(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|(?:[0-9a-fA-F]{1,4}:){1,7}:|(?:[0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|(?:[0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|(?:[0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|(?:[0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|(?:[0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])

This covers every valid IPv6 scenario: full addresses, compressed ones, link-local with zone IDs, and IPv4-mapped IPv6 addresses.

Step 2: Add the Prefix Matching Logic

Now, we just need to extend this base regex to handle the prefix part. There are two common scenarios:

Scenario 1: You Only Want to Match IPv6 Addresses With a Prefix

Append this pattern to the end of your base IPv6 regex:

\/(?:12[0-8]|1[01][0-9]|[1-9]?[0-9])

Breaking this down:

  • \/ escapes the forward slash (since it's a special character in regex)
  • 12[0-8] matches 120-128 (the upper limit for prefix lengths)
  • 1[01][0-9] matches 100-119
  • [1-9]?[0-9] matches 0-99 (the ? makes the first digit optional for single-digit numbers)

Combined with the base regex (and wrapped in anchors to ensure full string matches), it becomes:

^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|(?:[0-9a-fA-F]{1,4}:){1,7}:|(?:[0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|(?:[0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|(?:[0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|(?:[0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|(?:[0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\/(?:12[0-8]|1[01][0-9]|[1-9]?[0-9])$

Scenario 2: You Want to Match IPv6 Addresses With or Without a Prefix

Make the prefix part optional by adding a ? to the end of the prefix group:

(?:\/(?:12[0-8]|1[01][0-9]|[1-9]?[0-9]))?

The combined regex (with anchors) would look like this:

^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|(?:[0-9a-fA-F]{1,4}:){1,7}:|(?:[0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|(?:[0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|(?:[0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|(?:[0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|(?:[0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])(?:\/(?:12[0-8]|1[01][0-9]|[1-9]?[0-9]))?$

Fixing Your Existing Regex

Since your current regex works for three types of examples but fails on prefixed IPv6, here's a straightforward way to adjust it:

  1. Find the end of your IPv6 matching segment: Locate where your regex stops matching the core IPv6 address (look for anchors like $ or the end of a capturing group).
  2. Append the prefix pattern: Add either the required or optional prefix group to that segment.
  3. Check anchors: If you're using ^ and $ to match full strings, make sure the prefix part is included within those anchors—otherwise, the regex might stop matching before the prefix.

Quick Example Fix

Suppose your existing IPv6 regex is grouped like (your-ipv6-regex). Modify it to:

^(your-ipv6-regex)(?:\/(?:12[0-8]|1[01][0-9]|[1-9]?[0-9]))?$

The (?:...) is a non-capturing group (so it doesn't add extra capture groups you don't need), and the ? makes the prefix optional.

Test These Cases to Be Sure

Don't forget to validate with these examples to ensure your regex works as expected:

  • Valid prefixed IPv6: 2001:db8::/32, fe80::1%eth0/64, ::1/128, 2001:db8:3:4::192.0.2.33/120
  • Valid IPv6 without prefix: 2001:db8::, fe80::1%wlan0
  • Invalid prefixes (should not match): 2001:db8::/129 (too long), 2001:db8::/-5 (negative), 2001:db8::/abc (non-numeric)

内容的提问来源于stack exchange,提问作者Дімон Качуринець

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:06:56