普通防爆pression验证十六进制字符串 [英] Regular Expression to validate hex string

查看:148
本文介绍了普通防爆pression验证十六进制字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用这个简单的规律EX pression验证一个十六进制字符串 ^ [A-发f0-9] {16} $ ,你可以看到我使用一个量词,以验证字符串是16个字符长,我想知道如果我可以用另外一个量词在同一个正则表达式来验证字符串的长度是16或18(不是17)。

I'm using this simple regular expression to validate a hex string ^[A-Fa-f0-9]{16}$ as you can see I'm using a quantifier to validate that the string is 16 chars long, I was wondering if I can use another quantifier in the same regex to validate the string length to be either 16 or 18 (not 17).

在此先感谢:)

推荐答案

我相信

^([A-Fa-f0-9]{2}){8,9}$

将工作。

这是很好的,因为它推广到任何偶数长度的字符串。

This is nice because it generalizes to any even-length string.

编辑:基于一个很好的注释小更新

Small update based on a good comment.

这篇关于普通防爆pression验证十六进制字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆