使用正规表示法比对数字范围使用正规表示法比对数字范围使用正规表示法比对数字范围使用正规表示法比对数字范围
  • 文章
  • 正则表达式
    • 工具
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

搜索范围
模糊匹配
搜索标题
搜索内容
发表 admin at 2024年3月5日
类别
  • 正则表达式
标签
使用正则表达式比对数字范围
  • 简
  • 繁
  • En
关于正则表达式 » 正则表达式范例 » 使用正则表达式比对数字范围

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

使用正则表达式比对数字范围

由于 正则表达式 处理的是文本而非数字,因此要比对特定范围内的数字需要特别小心。您无法只写 [0-255] 来比对 0 到 255 之间的数字。虽然这是一个有效的正则表达式,但它比对的是完全不同的东西。[0-255] 是 字符类别,包含三个元素:字符范围 0-2、字符 5 和字符 5(再次出现)。此字符类别比对单一数字 0、1、2 或 5,就像 [0125] 一样。

由于正则表达式处理的是文本,因此正则表达式引擎会将 0 视为单一字符,而将 255 视为三个字符。若要比对 0 到 255 之间的所有字符,我们需要一个比对一个到三个字符的正则表达式。

正则表达式 [0-9] 符合 0 到 9 的单一数字。 [1-9][0-9] 符合 10 到 99 的双位数字。这是简单的部分。

符合三位数字稍微复杂一点,因为我们需要排除 256 到 999 的数字。 1[0-9][0-9] 符合 100 到 199。 2[0-4][0-9] 符合 200 到 249。最后,25[0-5] 加入 250 到 255。

如你所见,你需要将数字范围分成具有相同位数的范围,而这些范围中的每个范围都允许每个位数有相同的变化。在我们范例中的 3 位数字范围中,以 1 开头的数字允许后两个数字有 10 个数字,而以 2 开头的数字则限制后面的数字。

使用 交替 将所有这些组合在一起,我们得到:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]。这符合我们想要的数字,但有一个警告:正则表达式搜索通常允许部分符合,因此我们的正则表达式会在 12345 中符合 123。这有两个解决方案。

搜索数字范围

如果您在较大的文档或输入字符串中搜索这些数字,请使用 字词边界,以要求非字词字符(或完全没有字符)出现在任何有效配对之前和之后。正则表达式就会变成 \b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b。由于交替操作符具有最低优先级,因此需要 括号 将交替选项分组在一起。这样,正则表达式引擎会尝试配对第一个字词边界,然后尝试所有交替选项,最后尝试配对数字后面的第二个字词边界。正则表达式引擎会将所有字母数字字符以及底线视为字词字符。

验证数字范围

如果您使用正则表达式验证输入,您可能会想要检查整个输入是否包含有效的数字。为此,请将字词边界替换为 锚点,以配对字符串的开头和结尾:^([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$。

以下是您可能想要配对的几个更常见的范围

  • 000..255: ^([01][0-9][0-9]|2[0-4][0-9]|25[0-5])$
  • 0 或 000..255:^([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])$
  • 0 或 000..127:^(0?[0-9]?[0-9]|1[01][0-9]|12[0-7])$
  • 0..999: ^([0-9]|[1-9][0-9]|[1-9][0-9][0-9])$
  • 000..999: ^[0-9]{3}$
  • 0 或 000..999:^[0-9]{1,3}$
  • 1..999: ^([1-9]|[1-9][0-9]|[1-9][0-9][0-9])$
  • 001..999: ^(00[1-9]|0[1-9][0-9]|[1-9][0-9][0-9])$
  • 1 或 001..999:^(0{0,2}[1-9]|0?[1-9][0-9]|[1-9][0-9][0-9])$
  • 0 或 00..59:^[0-5]?[0-9]$
  • 0 或 000..366:^([012]?[0-9]?[0-9]|3[0-5][0-9]|36[0-6])$
使用正規表示法比對數字範圍
  • 简
  • 繁
  • En
關於正規表示式 » 正規表示式範例 » 使用正規表示法比對數字範圍

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

使用正規表示法比對數字範圍

由於 正規表示法 處理的是文字而非數字,因此要比對特定範圍內的數字需要特別小心。您無法只寫 [0-255] 來比對 0 到 255 之間的數字。雖然這是一個有效的正規表示法,但它比對的是完全不同的東西。[0-255] 是 字元類別,包含三個元素:字元範圍 0-2、字元 5 和字元 5(再次出現)。此字元類別比對單一數字 0、1、2 或 5,就像 [0125] 一樣。

由於正規表示法處理的是文字,因此正規表示法引擎會將 0 視為單一字元,而將 255 視為三個字元。若要比對 0 到 255 之間的所有字元,我們需要一個比對一個到三個字元的正規表示法。

正規表示式 [0-9] 符合 0 到 9 的單一數字。 [1-9][0-9] 符合 10 到 99 的雙位數字。這是簡單的部分。

符合三位數字稍微複雜一點,因為我們需要排除 256 到 999 的數字。 1[0-9][0-9] 符合 100 到 199。 2[0-4][0-9] 符合 200 到 249。最後,25[0-5] 加入 250 到 255。

如你所見,你需要將數字範圍分成具有相同位數的範圍,而這些範圍中的每個範圍都允許每個位數有相同的變化。在我們範例中的 3 位數字範圍中,以 1 開頭的數字允許後兩個數字有 10 個數字,而以 2 開頭的數字則限制後面的數字。

使用 交替 將所有這些組合在一起,我們得到:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]。這符合我們想要的數字,但有一個警告:正規表示式搜尋通常允許部分符合,因此我們的正規表示式會在 12345 中符合 123。這有兩個解決方案。

搜尋數字範圍

如果您在較大的文件或輸入字串中搜尋這些數字,請使用 字詞邊界,以要求非字詞字元(或完全沒有字元)出現在任何有效配對之前和之後。正規表示式就會變成 \b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b。由於交替運算子具有最低優先順序,因此需要 括號 將交替選項分組在一起。這樣,正規表示式引擎會嘗試配對第一個字詞邊界,然後嘗試所有交替選項,最後嘗試配對數字後面的第二個字詞邊界。正規表示式引擎會將所有字母數字字元以及底線視為字詞字元。

驗證數字範圍

如果您使用正規表示式驗證輸入,您可能會想要檢查整個輸入是否包含有效的數字。為此,請將字詞邊界替換為 錨點,以配對字串的開頭和結尾:^([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$。

以下是您可能想要配對的幾個更常見的範圍

  • 000..255: ^([01][0-9][0-9]|2[0-4][0-9]|25[0-5])$
  • 0 或 000..255:^([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])$
  • 0 或 000..127:^(0?[0-9]?[0-9]|1[01][0-9]|12[0-7])$
  • 0..999: ^([0-9]|[1-9][0-9]|[1-9][0-9][0-9])$
  • 000..999: ^[0-9]{3}$
  • 0 或 000..999:^[0-9]{1,3}$
  • 1..999: ^([1-9]|[1-9][0-9]|[1-9][0-9][0-9])$
  • 001..999: ^(00[1-9]|0[1-9][0-9]|[1-9][0-9][0-9])$
  • 1 或 001..999:^(0{0,2}[1-9]|0?[1-9][0-9]|[1-9][0-9][0-9])$
  • 0 或 00..59:^[0-5]?[0-9]$
  • 0 或 000..366:^([012]?[0-9]?[0-9]|3[0-5][0-9]|36[0-6])$
Matching Numeric Ranges with a Regular Expression
  • 简
  • 繁
  • En
About Regular Expressions » Sample Regular Expressions » Matching Numeric Ranges with a Regular Expression

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

Matching Numeric Ranges with a Regular Expression

Since regular expressions deal with text rather than with numbers, matching a number in a given range takes a little extra care. You can’t just write [0-255] to match a number between 0 and 255. Though a valid regex, it matches something entirely different. [0-255] is a character class with three elements: the character range 0-2, the character 5 and the character 5 (again). This character class matches a single digit 0, 1, 2 or 5, just like [0125].

Since regular expressions work with text, a regular expression engine treats 0 as a single character, and 255 as three characters. To match all characters from 0 to 255, we’ll need a regex that matches between one and three characters.

The regex [0-9] matches single-digit numbers 0 to 9. [1-9][0-9] matches double-digit numbers 10 to 99. That’s the easy part.

Matching the three-digit numbers is a little more complicated, since we need to exclude numbers 256 through 999. 1[0-9][0-9] takes care of 100 to 199. 2[0-4][0-9] matches 200 through 249. Finally, 25[0-5] adds 250 till 255.

As you can see, you need to split up the numeric range in ranges with the same number of digits, and each of those ranges that allow the same variation for each digit. In the 3-digit range in our example, numbers starting with 1 allow all 10 digits for the following two digits, while numbers starting with 2 restrict the digits that are allowed to follow.

Putting this all together using alternation we get: [0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]. This matches the numbers we want, with one caveat: regular expression searches usually allow partial matches, so our regex would match 123 in 12345. There are two solutions to this.

Searching for Numeric Ranges

If you’re searching for these numbers in a larger document or input string, use word boundaries to require a non-word character (or no character at all) to precede and to follow any valid match. The regex then becomes \b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b. Since the alternation operator has the lowest precedence of all, the parentheses are required to group the alternatives together. This way the regex engine will try to match the first word boundary, then try all the alternatives, and then try to match the second word boundary after the numbers it matched. Regular expression engines consider all alphanumeric characters, as well as the underscore, as word characters.

Validating Numeric Ranges

If you’re using the regular expression to validate input, you’ll probably want to check that the entire input consists of a valid number. To do this, replace the word boundaries with anchors to match the start and end of the string: ^([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$.

Here are a few more common ranges that you may want to match:

  • 000..255: ^([01][0-9][0-9]|2[0-4][0-9]|25[0-5])$
  • 0 or 000..255: ^([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])$
  • 0 or 000..127: ^(0?[0-9]?[0-9]|1[01][0-9]|12[0-7])$
  • 0..999: ^([0-9]|[1-9][0-9]|[1-9][0-9][0-9])$
  • 000..999: ^[0-9]{3}$
  • 0 or 000..999: ^[0-9]{1,3}$
  • 1..999: ^([1-9]|[1-9][0-9]|[1-9][0-9][0-9])$
  • 001..999: ^(00[1-9]|0[1-9][0-9]|[1-9][0-9][0-9])$
  • 1 or 001..999: ^(0{0,2}[1-9]|0?[1-9][0-9]|[1-9][0-9][0-9])$
  • 0 or 00..59: ^[0-5]?[0-9]$
  • 0 or 000..366: ^([012]?[0-9]?[0-9]|3[0-5][0-9]|36[0-6])$
©2015-2025 艾丽卡 support@alaica.com