求可匹配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 beforeF)F: Matches the required uppercase prefix (use[Ff]or add theimodifier if you need to support lowercasef)(?:[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.01orF12.34), use theimodifier (e.g., in JavaScript:/^F(?:[1-9]\d?|)\.\d{2}$/i)
内容的提问来源于stack exchange,提问作者Morfyus
相关产品推荐
相关产品推荐

