gpt4 book ai didi

java - 用于限制验证十六进制值从 0000 到 7F7F 的正则表达式

转载 作者:行者123 更新时间:2023-12-01 21:15:25 24 4
gpt4 key购买 nike

在下面的代码中,我验证字符串是否是有效的十六进制有没有办法验证十六进制值是否限制为 0000 到 7F7F 之间的值?

value.matches("[0-9a-fA-F]+")

最佳答案

您可以将值转换为 int 并使用常规 <>运算符:

public class Main {

public static void main(String[] args) {

final int LOW = 0x0000;
final int HIGH = 0x7F7F;
String sample1 = "A1A1";
String sample2 = "2ABC";

int hex1 = Integer.decode("0x"+sample1);
System.out.println(LOW < hex1 && hex1 < HIGH);
int hex2 = Integer.decode("0x"+sample2);
System.out.println(LOW < hex2 && hex2 < HIGH);
}
}

输出:

false
true

关于java - 用于限制验证十六进制值从 0000 到 7F7F 的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40274363/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com