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

正则表达式:对\b边界行为的困惑及十六进制匹配问题

Solution for Dual Hex Number Matching in Atom Syntax Rules

Got it, let's fix this regex issue for your Atom custom syntax highlighting. The core problem here is that \b (word boundary) behaves differently for $ vs 0x—since $ isn't a word character (word chars are a-z, A-Z, 0-9, _), \b doesn't work as expected to block unwanted preceding characters for the $ prefix.

Here's the single regex that will handle both cases correctly, avoiding false matches like t0x0t:

(?<!\w)(?:\$|0x)[A-Fa-f0-9]+

Breakdown of how this works:

  • (?<!\w): A negative lookbehind assertion that ensures there is no word character immediately before our hex prefix. This blocks cases like t0x0t (where t is a word character before 0x) and also prevents unwanted matches like a$123 (where a is a word character before $).
  • (?:\$|0x): A non-capturing group that matches either the $ or 0x prefix. Using ?: avoids creating an unnecessary capture group, which is cleaner for syntax highlighting.
  • [A-Fa-f0-9]+: Matches one or more hexadecimal digits (upper/lowercase letters + numbers).

Test cases this handles correctly:

  • ✅ Matches $affe (whether at the start of a line or after a non-word character like !$affe)
  • ✅ Matches 0xeffa
  • ❌ Does NOT match t0x0t (the t before 0x triggers the negative lookbehind, so the regex skips this)
  • ❌ Does NOT match foo$123 (the o before $ is a word character, so the lookbehind blocks the match)

Why this is better than using \b:
\b relies on the transition between word and non-word characters. For 0x, \b0x works because 0 is a word character—if there's a word character before it (like t), there's no word boundary between t and 0, so it doesn't match. But for $, \b$ would actually match cases like t$affe (since t is a word character and $ is non-word, creating a boundary), which we don't want. The negative lookbehind fixes this by explicitly checking for the absence of a preceding word character, regardless of the prefix type.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 10:13:38