如何寻找或验证 IP 地址如何寻找或验证 IP 地址如何寻找或验证 IP 地址如何寻找或验证 IP 地址
  • 文章
  • 正则表达式
    • 工具
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

搜索范围
模糊匹配
搜索标题
搜索内容
发表 admin at 2024年3月5日
类别
  • 正则表达式
标签
如何寻找或验证 IP 地址
  • 简
  • 繁
  • En
关于正则表达式 » 正则表达式范例 » 如何寻找或验证 IP 地址

范例
正则表达式范例
数字范围
浮点数
电子邮件地址
IP 地址
有效的日期
数字日期转文本
信用卡号码
比对完整行
删除重复行
编程
两个相近的字词
陷阱
灾难性回溯
过多重复
阻断服务攻击
让所有内容都可选
重复捕获组
混合 Unicode 和 8 比特
更多本网站信息
简介
正则表达式快速入门
正则表达式教程
替换字符串教程
应用程序和语言
正则表达式范例
正则表达式参考
替换字符串参考

如何寻找或验证 IP 地址

比对 IP 地址是正则表达式复杂度和精确度之间权衡取舍的另一个好范例。 \b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b 会完美比对任何 IP 地址。但也会比对 999.999.999.999,好像它是一个有效的 IP 地址。如果你的正则表达式版本支持 Unicode,它甚至可能会比对 ١٢٣.१२३.೧೨೩.๑๒๓。这是否会造成问题,取决于你打算套用正则表达式的文件或数据。

限制和截取 IP 地址的四个数字

若要将 IP 地址中的所有 4 个数字限制在 0 到 255 之间,你可以使用下列正则表达式。它会将 IP 地址的 4 个数字保存到 捕获组 中。你可以使用这些群组进一步处理 IP 号码。自由间距模式 允许它符合页面宽度。

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

上述正则表达式允许数字 10 到 99 有 1 个前导零,数字 0 到 9 有 2 个前导零。严格来说,具有前导零的 IP 地址表示八进位表示法。因此,您可能想要禁止前导零。这需要一个稍长的正则表达式

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

限制四个 IP 地址数字而不截取它们

如果您不需要访问个别数字,您可以使用 量词 将上述 3 个正则表达式缩短为

\b(?:\d{1,3}\.){3}\d{1,3}\b

\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}
  
(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b

\b(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.){3}
  
(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\b

检查用户输入

上述 regex 使用 字词边界 来确保 IP 地址中的第一个和最后一个数字不是字母数字字符串行中的一部分。这些 regex 适用于在较长的字符串中寻找 IP 地址。

如果您想要验证用户输入,确保字符串仅包含 IP 地址,则需要将字词边界替换为 字符串开头和字符串结尾锚点。如果您的 regex 风格支持,可以使用专用的锚点 \A 和 \z

\A(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}
  
(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\z

如果不是,您必须使用 ^ 和 $,并确保它们在断行处比对的选项已关闭

^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}
 (?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$

如何尋找或驗證 IP 位址
  • 简
  • 繁
  • En
關於正規表示式 » 正規表示式範例 » 如何尋找或驗證 IP 位址

範例
正規表示式範例
數字範圍
浮點數
電子郵件地址
IP 位址
有效的日期
數字日期轉文字
信用卡號碼
比對完整行
刪除重複行
程式設計
兩個相近的字詞
陷阱
災難性回溯
過多重複
阻斷服務攻擊
讓所有內容都可選
重複擷取群組
混合 Unicode 和 8 位元
更多本網站資訊
簡介
正規表示式快速入門
正規表示式教學
替換字串教學
應用程式和語言
正規表示式範例
正規表示式參考
替換字串參考

如何尋找或驗證 IP 位址

比對 IP 位址是正規表示式複雜度和精確度之間權衡取捨的另一個好範例。 \b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b 會完美比對任何 IP 位址。但也會比對 999.999.999.999,好像它是一個有效的 IP 位址。如果你的正規表示式版本支援 Unicode,它甚至可能會比對 ١٢٣.१२३.೧೨೩.๑๒๓。這是否會造成問題,取決於你打算套用正規表示式的檔案或資料。

限制和擷取 IP 位址的四個數字

若要將 IP 位址中的所有 4 個數字限制在 0 到 255 之間,你可以使用下列正規表示式。它會將 IP 位址的 4 個數字儲存到 擷取群組 中。你可以使用這些群組進一步處理 IP 號碼。自由間距模式 允許它符合頁面寬度。

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

上述正規表示法允許數字 10 到 99 有 1 個前導零,數字 0 到 9 有 2 個前導零。嚴格來說,具有前導零的 IP 位址表示八進位表示法。因此,您可能想要禁止前導零。這需要一個稍長的正規表示法

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

限制四個 IP 位址數字而不擷取它們

如果您不需要存取個別數字,您可以使用 量詞 將上述 3 個正規表示法縮短為

\b(?:\d{1,3}\.){3}\d{1,3}\b

\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}
  
(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b

\b(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.){3}
  
(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\b

檢查使用者輸入

上述 regex 使用 字詞邊界 來確保 IP 位址中的第一個和最後一個數字不是字母數字字元序列中的一部分。這些 regex 適用於在較長的字串中尋找 IP 位址。

如果您想要驗證使用者輸入,確保字串僅包含 IP 位址,則需要將字詞邊界替換為 字串開頭和字串結尾錨點。如果您的 regex 風格支援,可以使用專用的錨點 \A 和 \z

\A(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}
  
(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\z

如果不是,您必須使用 ^ 和 $,並確保它們在斷行處比對的選項已關閉

^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}
 (?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$

How to Find or Validate an IP Address
  • 简
  • 繁
  • En
About Regular Expressions » Sample Regular Expressions » How to Find or Validate an IP Address

Examples
Regular Expressions Examples
Numeric Ranges
Floating Point Numbers
Email Addresses
IP Addresses
Valid Dates
Numeric Dates to Text
Credit Card Numbers
Matching Complete Lines
Deleting Duplicate Lines
Programming
Two Near Words
Pitfalls
Catastrophic Backtracking
Too Many Repetitions
Denial of Service
Making Everything Optional
Repeated Capturing Group
Mixing Unicode & 8-bit
More on This Site
Introduction
Regular Expressions Quick Start
Regular Expressions Tutorial
Replacement Strings Tutorial
Applications and Languages
Regular Expressions Examples
Regular Expressions Reference
Replacement Strings Reference

How to Find or Validate an IP Address

Matching an IP address is another good example of a trade-off between regex complexity and exactness. \b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b will match any IP address just fine. But will also match 999.999.999.999 as if it were a valid IP address. If your regex flavor supports Unicode, it may even match ١٢٣.१२३.೧೨೩.๑๒๓. Whether this is a problem depends on the files or data you intend to apply the regex to.

Restricting and Capturing The Four IP Address Numbers

To restrict all 4 numbers in the IP address to 0..255, you can use the following regex. It stores each of the 4 numbers of the IP address into a capturing group. You can use these groups to further process the IP number. Free-spacing mode allows this to fit the width of the page.

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

The above regex allows one leading zero for numbers 10 to 99 and up to two leading zeros for numbers 0 to 9. Strictly speaking, IP addresses with leading zeros imply octal notation. So you may want to disallow leading zeros. This requires a slightly longer regex:

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

Restricting The Four IP Address Numbers Without Capturing Them

If you don’t need access to the individual numbers, you can shorten above 3 regexes with a quantifier to:

\b(?:\d{1,3}\.){3}\d{1,3}\b

\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}
  
(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b

\b(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.){3}
  
(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\b

Checking User Input

The above regexes use word boundaries to make sure the first and last number in the IP address aren’t part of a longer sequence of alphanumeric characters. These regexes are appropriate for finding IP addresses in longer strings.

If you want to validate user input by making sure a string consists of nothing but an IP address then you need to replace the word boundaries with start-of-string and end-of-string anchors. You can use the dedicated anchors \A and \z if your regex flavor supports them:

\A(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}
  
(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\z

If not, you’ll have to use ^ and $ and make sure that the option for them to match at line breaks is off:

^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}
 (?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$

©2015-2025 艾丽卡 support@alaica.com