多语言环境下的正则表达式实战:校验整数、小数

在软件开发中,正则表达式是验证用户输入数据格式的强大工具,特别是在处理表单验证时。本文将通过JavaScript、Java、Python、C、Rust、Go、C++六种编程语言展示如何使用正则表达式来校验输入是否为整数或小数,特别强调小数点后最多保留两位的场景。让我们一起探索如何在不同语言中实现这一功能。

JavaScript 示例

const regex = /^[0-9]+(\.[0-9]{1,2})?$/;
const isValid = regex.test(inputString);
console.log(isValid); // 输出 true 或 false

Java 示例

import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class Main {
    public static void main(String[] args) {
        Pattern pattern = Pattern.compile("^[0-9]+(\\.[0-9]{1,2})?");
        Matcher matcher = pattern.matcher(inputString);
        System.out.println(matcher.matches()); // 输出 true 或 false
    }
}

Python 示例

import re

pattern = r'^[0-9]+(\.[0-9]{1,2})?$'
is_valid = bool(re.match(pattern, input_string))
print(is_valid) # 输出 True 或 False

C 示例

#include <stdio.h>
#include <regex.h>

int main() {
    regex_t preg;
    int reti;
    char input[] = "your_input";
    char pattern[] = "^[0-9]+(\\.[0-9]{1,2})?$";

    reti = regcomp(&preg, pattern, REG_EXTENDED);
    if (reti)
        fprintf(stderr, "Could not compile regex\n");
    else if (!regexec(&preg, input, 0, NULL, 0, 0))
        printf("Matched\n");
    else
        printf("Not matched\n");

    regfree(&preg);
    return 0;
}

Rust 示例

fn main() {
    let re = regex::Regex::new(r"^[\d]+(\.\d{1,2})?$").unwrap();
    let is_match = re.is_match("your_input");
    println!("{}", is_match); // 输出 true 或 false
}

Go 示例

package main

import (
	"fmt"
	"regexp"
)

func main() {
	pattern := `^[0-9]+(\.[0-9]{1,2})?`
	matched, _ := regexp.MatchString(pattern, "your_input")
	fmt.Println(matched) // 输出 true 或 false
}

C++ 示例

#include <regex>
#include <iostream>

int main() {
    std::regex pattern(R"(^[0-9]+(\.[0-9]{1,2})?)$)");
    std::string input = "your_input";
    bool match = std::regex_match(input, pattern);
    std::cout << (match ? "Matched" : "Not matched") << std::endl;
    return 0;
}

以上示例展示了在不同编程语言中,通过正则表达式^[0-9]+(\.[0-9]{1,2})?$来校验一个字符串是否符合整数或最多保留两位小数的正数格式。此正则表达式从开始(^)匹配一个或多个数字([0-9]+),随后是可选的((...))小数点(\.)后跟一或两个数字(\d{1,2}),最后是结束符($)。每个示例中,将"your_input"替换为实际需要校验的字符串。

相关推荐

  1. 【C++语言表达式

    2024-05-10 08:30:10       22 阅读
  2. 表达式语法详解

    2024-05-10 08:30:10       37 阅读
  3. Python 表达式语法

    2024-05-10 08:30:10       27 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-10 08:30:10       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-10 08:30:10       106 阅读
  3. 在Django里面运行非项目文件

    2024-05-10 08:30:10       87 阅读
  4. Python语言-面向对象

    2024-05-10 08:30:10       96 阅读

热门阅读

  1. cpp Easy Timer

    2024-05-10 08:30:10       33 阅读
  2. 华为ICT学院教师指南(HCAI认证)结课测试

    2024-05-10 08:30:10       40 阅读
  3. 可视化3个10分类

    2024-05-10 08:30:10       38 阅读
  4. 线程池介绍

    2024-05-10 08:30:10       24 阅读
  5. 第4章 Vim编辑器与Shell命令脚本

    2024-05-10 08:30:10       39 阅读
  6. CONFIG_INITRAMFS_SOURCE

    2024-05-10 08:30:10       34 阅读
  7. mac重装pycharm失败

    2024-05-10 08:30:10       38 阅读
  8. 跨平台应用开发神器Uniapp

    2024-05-10 08:30:10       34 阅读
  9. Go语言函数

    2024-05-10 08:30:10       33 阅读