[英]Regex to check string contains only Hex characters
I have never done regex before, and I have seen they are very useful for working with strings. 我以前从未做过正则表达式,而且我发现它们对于处理字符串非常有用。 I saw a few tutorials (for example) but I still cannot understand how to make a simple Java regex check for hexadecimal characters in a string.
我看过一些教程 (例如),但是我仍然不明白如何对字符串中的十六进制字符进行简单的Java正则表达式检查。
The user will input in the text box something like: 0123456789ABCDEF
and I would like to know that the input was correct otherwise if something like XTYSPG456789ABCDEF
when return false. 用户将在文本框中输入以下内容:
0123456789ABCDEF
,我想知道该输入是正确的,否则,当返回false时,例如XTYSPG456789ABCDEF
。
Is it possible to do that with a regex or did I misunderstand how they work? 是否可以使用正则表达式来做到这一点,或者我是否误解了它们的工作原理?
Yes, you can do that with a regular expression: 是的,您可以使用正则表达式来做到这一点:
^[0-9A-F]+$
Explanation: 说明:
^ Start of line. [0-9A-F] Character class: Any character in 0 to 9, or in A to F. + Quantifier: One or more of the above. $ End of line.
To use this regular expression in Java you can for example call the matches
method on a String: 要在Java中使用此正则表达式,您可以例如在String上调用
matches
方法:
boolean isHex = s.matches("[0-9A-F]+");
Note that matches
finds only an exact match so you don't need the start and end of line anchors in this case. 请注意,
matches
仅查找完全匹配项,因此在这种情况下,您不需要行锚的开始和结束。 See it working online: ideone 看到它在线上工作: ideone
You may also want to allow both upper and lowercase AF, in which case you can use this regular expression: 您可能还希望同时允许大写和小写AF,在这种情况下,您可以使用以下正则表达式:
^[0-9A-Fa-f]+$
May be you want to use the POSIX character class \\p{XDigit}
, so: 可能是您想使用POSIX字符类
\\p{XDigit}
,所以:
^\\p{XDigit}+$
Additionally, if you plan to use the regular expression very often, it is recommended to use a constant in order to avoid recompile it each time, eg: 另外,如果您打算非常频繁地使用正则表达式,建议使用常量以避免每次都重新编译它,例如:
private static final Pattern REGEX_PATTERN =
Pattern.compile("^\\p{XDigit}+$");
public static void main(String[] args) {
String input = "0123456789ABCDEF";
System.out.println(
REGEX_PATTERN.matcher(input).matches()
); // prints "true"
}
Actually, the given answer is not totally correct. 实际上,给出的答案并不完全正确。 The problem arises because the numbers 0-9 are also decimal values.
出现问题是因为数字0-9也是十进制值。 PART of what you have to do is to test for 00-99 instead of just 0-9 to ensure that the lower values are not decimal numbers.
您要做的部分工作是测试00-99,而不是0-9,以确保较低的值不是十进制数。 Like so:
像这样:
^([0-9A-Fa-f]{2})+$
To say these have to come in pairs! 要说这些必须成对出现! Otherwise - the string is something else!
否则-字符串不一样! :-)
:-)
Example: 例:
(Pick one)
var a = "1e5";
var a = "10";
var a = "314159265";
If I used the accepted answer in a regular expression it would return TRUE. 如果我在正则表达式中使用了接受的答案,它将返回TRUE。
var re1 = new RegExp( /^[0-9A-Fa-f]+$/ );
var re2 = new RegExp( /^([0-9A-Fa-f]{2})+$/ );
if( re1.test(a) ){ alert("#1 = This is a hex value!"); }
if( re2.test(a) ){ alert("#2 = This IS a hex string!"); }
else { alert("#2 = This is NOT a hex string!"); }
Note that the "10" returns TRUE in both cases. 请注意,在两种情况下,“ 10”均返回TRUE。 If an incoming string only has 0-9 you can NOT tell, easily if it is a hex value or a decimal value UNLESS there is a missing zero in front of off length strings (hex values always come in pairs - ie - Low byte/high byte).
如果传入的字符串只有0-9,则您无法分辨,如果是十六进制值或十进制值,则很容易看出来,除非长度不一的字符串前面缺少零(十六进制值始终成对出现-即-低字节/高位字节)。 But values like "34" are both perfectly valid decimal OR hexadecimal numbers.
但是像“ 34”这样的值都是完全有效的十进制或十六进制数字。 They just mean two different things.
他们只是意味着两件事。
Also note that "3.14159265" is not a hex value no matter which test you do because of the period. 还请注意,由于周期,无论您执行哪种测试,“ 3.14159265”都不是十六进制值。 But with the addition of the "{2}" you at least ensure it really is a hex string rather than something that LOOKS like a hex string.
但是,通过添加“ {2}”,您至少要确保它确实是一个十六进制字符串,而不是像十六进制字符串那样看起来。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.