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

加载更多搜索结果...

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

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

使用正则表达式比对浮点数

这个范例说明如何避免不熟悉 正则表达式 的人常犯的常见错误。例如,我们将尝试创建一个正则表达式,可以比对任何浮点数。我们的正则表达式也应该比对整数和浮点数,其中未提供整数部分。我们不会尝试比对具有指数的数字,例如 1.5e8(科学记号中的 1.5 亿)。

乍看之下,下列正则表达式似乎可以达到目的:[-+]?[0-9]*\.?[0-9]*。这将浮点数定义为一个 可选 符号,后接一个 系列 数字(整数部分)的 可选 项目,后接一个可选的句点,后接另一个可选的数字系列(小数部分)。

将正则表达式用文本拼出来会很明显:此正则表达式中的所有内容都是可选的。此正则表达式将符号本身或点本身视为有效的浮点数。事实上,它甚至将空字符串视为有效的浮点数。如果你尝试使用此正则表达式在文件中寻找浮点数,你会在字符串中没有浮点数出现的每个位置取得零长度比对。

未转义点也是常见的错误。未转义的点会比对任何字符,包括点。如果我们没有转义点,4.4 和 4X4 都会被视为浮点数。

在创建正则表达式时,考量它不应该比对什么比它应该比对什么更重要。上述正则表达式确实比对正确的浮点数,因为正则表达式引擎是贪婪的。但它也比对许多我们不想要的内容,我们必须排除这些内容。

以下是更好的尝试:[-+]?([0-9]*\.[0-9]+|[0-9]+)。此正则表达式比对可选符号,即不是后接零个或多个数字,后接一个点和一个或多个数字(浮点数,可选整数部分),就是后接一个或多个数字(整数)。

这是更好的定义。任何比对都必须包含至少一个数字。没有办法绕过[0-9]+部分。我们已成功排除我们不想要的比对:没有数字的比对。

我们可以优化此正则表达式为:[-+]?[0-9]*\.?[0-9]+。

如果你也想比对带有指数的数字,你可以使用:[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?。请注意,我如何通过群组,而不是让指数中的每个元素可选,让整个指数部分可选。

最后,如果你想验证特定字符串是否包含浮点数,而不是在较长的文本中寻找浮点数,你必须锚定你的正则表达式:^[-+]?[0-9]*\.?[0-9]+$ 或 ^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$。

使用正規表示式比對浮點數
  • 简
  • 繁
  • En
關於正規表示式 » 正規表示式範例 » 使用正規表示式比對浮點數

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

使用正規表示式比對浮點數

這個範例說明如何避免不熟悉 正規表示式 的人常犯的常見錯誤。例如,我們將嘗試建立一個正規表示式,可以比對任何浮點數。我們的正規表示式也應該比對整數和浮點數,其中未提供整數部分。我們不會嘗試比對具有指數的數字,例如 1.5e8(科學記號中的 1.5 億)。

乍看之下,下列正規表示式似乎可以達到目的:[-+]?[0-9]*\.?[0-9]*。這將浮點數定義為一個 可選 符號,後接一個 系列 數字(整數部分)的 可選 項目,後接一個可選的句點,後接另一個可選的數字系列(小數部分)。

將正規表示式用文字拼出來會很明顯:此正規表示式中的所有內容都是可選的。此正規表示式將符號本身或點本身視為有效的浮點數。事實上,它甚至將空字串視為有效的浮點數。如果你嘗試使用此正規表示式在檔案中尋找浮點數,你會在字串中沒有浮點數出現的每個位置取得零長度比對。

未跳脫點也是常見的錯誤。未跳脫的點會比對任何字元,包括點。如果我們沒有跳脫點,4.4 和 4X4 都會被視為浮點數。

在建立正規表示式時,考量它不應該比對什麼比它應該比對什麼更重要。上述正規表示式確實比對正確的浮點數,因為正規表示式引擎是貪婪的。但它也比對許多我們不想要的內容,我們必須排除這些內容。

以下是更好的嘗試:[-+]?([0-9]*\.[0-9]+|[0-9]+)。此正規表示式比對可選符號,即不是後接零個或多個數字,後接一個點和一個或多個數字(浮點數,可選整數部分),就是後接一個或多個數字(整數)。

這是更好的定義。任何比對都必須包含至少一個數字。沒有辦法繞過[0-9]+部分。我們已成功排除我們不想要的比對:沒有數字的比對。

我們可以最佳化此正規表示式為:[-+]?[0-9]*\.?[0-9]+。

如果你也想比對帶有指數的數字,你可以使用:[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?。請注意,我如何透過群組,而不是讓指數中的每個元素可選,讓整個指數部分可選。

最後,如果你想驗證特定字串是否包含浮點數,而不是在較長的文字中尋找浮點數,你必須錨定你的正規表示式:^[-+]?[0-9]*\.?[0-9]+$ 或 ^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$。

Matching Floating Point Numbers with a Regular Expression
  • 简
  • 繁
  • En
About Regular Expressions » Sample Regular Expressions » Matching Floating Point Numbers 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 Floating Point Numbers with a Regular Expression

This example shows how you can avoid a common mistake often made by people inexperienced with regular expressions. As an example, we will try to build a regular expression that can match any floating point number. Our regex should also match integers and floating point numbers where the integer part is not given. We will not try to match numbers with an exponent, such as 1.5e8 (150 million in scientific notation).

At first thought, the following regex seems to do the trick: [-+]?[0-9]*\.?[0-9]*. This defines a floating point number as an optional sign, followed by an optional series of digits (integer part), followed by an optional dot, followed by another optional series of digits (fraction part).

Spelling out the regex in words makes it obvious: everything in this regular expression is optional. This regular expression considers a sign by itself or a dot by itself as a valid floating point number. In fact, it even considers an empty string as a valid floating point number. If you tried to use this regex to find floating point numbers in a file, you’d get a zero-length match at every position in the string where no floating point number occurs.

Not escaping the dot is also a common mistake. A dot that is not escaped matches any character, including a dot. If we had not escaped the dot, both 4.4 and 4X4 would be considered floating point numbers.

When creating a regular expression, it is more important to consider what it should not match, than what it should. The above regex indeed matches a proper floating point number, because the regex engine is greedy. But it also matches many things we do not want, which we have to exclude.

Here is a better attempt: [-+]?([0-9]*\.[0-9]+|[0-9]+). This regular expression matches an optional sign, that is either followed by zero or more digits followed by a dot and one or more digits (a floating point number with optional integer part), or that is followed by one or more digits (an integer).

This is a far better definition. Any match must include at least one digit. There is no way around the [0-9]+ part. We have successfully excluded the matches we do not want: those without digits.

We can optimize this regular expression as: [-+]?[0-9]*\.?[0-9]+.

If you also want to match numbers with exponents, you can use: [-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?. Notice how I made the entire exponent part optional by grouping it together, rather than making each element in the exponent optional.

Finally, if you want to validate if a particular string holds a floating point number, rather than finding a floating point number within longer text, you’ll have to anchor your regex: ^[-+]?[0-9]*\.?[0-9]+$ or ^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$.

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