You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

网站项目中十六进制数合法格式校验的正则表达式实现咨询

Solution: Mutually Exclusive Hexadecimal Format Regex

Got it, let's tackle this regex problem step by step. You need to match three distinct, mutually exclusive hexadecimal formats, and your current regex only covers one case—here's how to fix it and enforce the "only one identifier allowed" rule:

First, let's lock in the requirements again to make sure we cover every edge case:

  • Legitimate formats (only one can be present per string):
    1. Prefix: 0x followed by one or more hex characters (0-9, A-F, a-f)
    2. Prefix: $ followed by one or more hex characters
    3. Suffix: h (or H for case insensitivity) preceded by one or more hex characters
  • No bare hex digits (like A without any prefix/suffix), no overlapping identifiers (like 0xa$ or $0xA)

Final Regex

^(0x[0-9A-Fa-f]+|\$[0-9A-Fa-f]+|[0-9A-Fa-f]+[hH])$

Breakdown of the Regex

Let's unpack how this enforces your rules:

  1. ^ and $: These anchor the regex to the start and end of the entire string. This is critical—it ensures we're matching the full input, not just a substring, which blocks invalid cases like 0xa$ (extra characters at the end) or $0xA (conflicting prefixes).
  2. Three mutually exclusive branches separated by |:
    • 0x[0-9A-Fa-f]+: Matches the 0x prefix format. The + fixes your original regex's limitation by requiring at least one hex character after the prefix.
    • \$[0-9A-Fa-f]+: Matches the $ prefix format. We escape $ with \ because it's a special regex character (used to mark the end of a string). The + ensures there's at least one hex digit after the $.
    • [0-9A-Fa-f]+[hH]: Matches the h suffix format. Here, we require one or more hex characters first, followed by either h or H (remove the H if you only want to allow lowercase h).
  3. Mutual exclusivity: Each branch is a complete, self-contained format, so there's no way for two identifiers to coexist. For example, $0xA won't match any branch—since the second branch expects $ to be followed directly by hex digits, not another prefix like 0x.

Testing Against Your Examples

Valid Cases (Will Match)

  • 0xA, 0xa, 0x101, 0x747: Matches the first branch
  • 101h: Matches the third branch
  • $747: Matches the second branch

Invalid Cases (Will Not Match)

  • A: No identifier present, doesn't match any branch
  • 0xa$: Ends with $, which isn't part of the first branch's structure
  • $0xA: $ is followed by 0x (not raw hex digits), so it doesn't match the second branch

Optional Adjustments

  • If you only want to allow lowercase h for the suffix, change [hH] to h
  • If you need to tolerate leading/trailing whitespace (your examples don't mention this, but it's a common edge case), add \s* at the start and end: ^\s*(0x[0-9A-Fa-f]+|\$[0-9A-Fa-f]+|[0-9A-Fa-f]+[hH])\s*$

内容的提问来源于stack exchange,提问作者flying_loaf_3

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 12:32:27