小数匹配

2024-06-23 21:29:21 416
匹配小数的正则表达式

正则表达式

/^(-?[1-9]\d*\.\d+|-?0\.\d*[1-9])$/

解析

  • ^:匹配字符串的开始。

  • (-?[1-9]\d.\d+|-?0.\d[1-9])**:

    • -?:匹配零个或一个负号。
    • [1-9]\d*\.\d+
      • [1-9]:匹配1到9之间的任意一个数字。
      • \d*:匹配零个或多个数字(0-9)。
      • \.:匹配一个点字符。
      • \d+:匹配一个或多个数字(0-9),表示小数部分。
    • |:逻辑或,表示前后两部分中的任意一个都可以匹配。
    • -?0\.\d*[1-9]
      • -?:匹配零个或一个负号。
      • 0:匹配数字0。
      • \.:匹配一个点字符。
      • \d*:匹配零个或多个数字(0-9)。
      • [1-9]:匹配1到9之间的任意一个数字,确保小数部分不全为0。
  • $:匹配字符串的结束。

示例

匹配成功的示例:

  • "1.23"
  • "-1.23"
  • "0.123"
  • "-0.123"

匹配失败的示例:

  • "123"(整数没有小数点)
  • "0.00"(小数部分全为0)
  • "-0.00"(小数部分全为0)
  • "abc"(非数字)

用法

Python

import re

pattern = r'^(-?[1-9]\d*\.\d+|-?0\.\d*[1-9])$'
test_strings = ["1.23", "-1.23", "0.123", "-0.123", "123", "0.00", "-0.00", "abc"]

for test_string in test_strings:
    if re.match(pattern, test_string):
        print(f"'{test_string}': 匹配成功")
    else:
        print(f"'{test_string}': 匹配失败")

JavaScript

const pattern = /^(-?[1-9]\d*\.\d+|-?0\.\d*[1-9])$/;
const testStrings = ["1.23", "-1.23", "0.123", "-0.123", "123", "0.00", "-0.00", "abc"];

testStrings.forEach(testString => {
    if (pattern.test(testString)) {
        console.log(`'${testString}': 匹配成功`);
    } else {
        console.log(`'${testString}': 匹配失败`);
    }
});

Java

import java.util.regex.*;

public class RegexTest {
    public static void main(String[] args) {
        String pattern = "^(-?[1-9]\\d*\\.\\d+|-?0\\.\\d*[1-9])$";
        String[] testStrings = {"1.23", "-1.23", "0.123", "-0.123", "123", "0.00", "-0.00", "abc"};
        
        for (String testString : testStrings) {
            if (Pattern.matches(pattern, testString)) {
                System.out.println("'" + testString + "': 匹配成功");
            } else {
                System.out.println("'" + testString + "': 匹配失败");
            }
        }
    }
}

C#

using System;
using System.Text.RegularExpressions;

class Program {
    static void Main() {
        string pattern = @"^(-?[1-9]\d*\.\d+|-?0\.\d*[1-9])$";
        string[] testStrings = {"1.23", "-1.23", "0.123", "-0.123", "123", "0.00", "-0.00", "abc"};
        
        foreach (string testString in testStrings) {
            if (Regex.IsMatch(testString, pattern)) {
                Console.WriteLine($"'{testString}': 匹配成功");
            } else {
                Console.WriteLine($"'{testString}': 匹配失败");
            }
        }
    }
}

PHP

<?php
$pattern = '/^(-?[1-9]\d*\.\d+|-?0\.\d*[1-9])$/';
$testStrings = ["1.23", "-1.23", "0.123", "-0.123", "123", "0.00", "-0.00", "abc"];

foreach ($testStrings as $testString) {
    if (preg_match($pattern, $testString)) {
        echo "'$testString': 匹配成功\n";
    } else {
        echo "'$testString': 匹配失败\n";
    }
}
?>

Ruby

pattern = /^(-?[1-9]\d*\.\d+|-?0\.\d*[1-9])$/
test_strings = ["1.23", "-1.23", "0.123", "-0.123", "123", "0.00", "-0.00", "abc"]

test_strings.each do |test_string|
  if pattern.match?(test_string)
    puts "'#{test_string}': 匹配成功"
  else
    puts "'#{test_string}': 匹配失败"
  end
end