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

求可匹配F.01、F1.01等格式及所有两位小数的正则表达式

Regex to Match F-prefixed Numeric Strings with Two Decimal Places

Got it, let's work through this regex requirement. You need to match strings starting with F, followed by an optional integer part (including cases where there's no integer before the decimal), then a decimal point, and exactly two decimal digits. Here's a solution tailored to your examples and covers all in-between cases:

^F(?:[1-9]\d?|)\.\d{2}$

Breakdown of the Regex Components:

  • ^ : Anchors the match to the start of the string (ensures no extra characters before F)
  • F : Matches the required uppercase prefix (use [Ff] or add the i modifier if you need to support lowercase f)
  • (?:[1-9]\d?|) : A non-capturing group for the optional integer part:
    • [1-9]\d? : Matches integers from 1 to 99 (1-digit numbers 1-9, or 2-digit numbers 10-99)
    • | : Acts as an "or" operator
    • Empty string: Allows cases where there's no integer before the decimal (like F.01)
  • \. : Matches the literal decimal point (escaped because . is a wildcard in regex)
  • \d{2} : Matches exactly two digits (00-99) to enforce two decimal places
  • $ : Anchors the match to the end of the string (ensures no extra characters after the decimal digits)

Test Cases That Match:

  • F.01 ✅
  • F1.01 ✅
  • F9.99 ✅
  • F10.01 ✅
  • F99.99 ✅

Adjustments for Edge Cases (if needed):

  • If you want to allow F0.01 (integer part is 0), modify the integer group to (?:\d*|) → updated regex: ^F(?:\d*|)\.\d{2}$
  • If you need to support longer integer values (like F123.45), change [1-9]\d? to [1-9]\d* → regex: ^F(?:[1-9]\d*|)\.\d{2}$
  • For case-insensitive matching (allow f.01 or F12.34), use the i modifier (e.g., in JavaScript: /^F(?:[1-9]\d?|)\.\d{2}$/i)

内容的提问来源于stack exchange,提问作者Morfyus

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.13 07:22:36