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

如何编写匹配指定格式的带国家代码英国电话号码的RegEx?

Strict Regex for Format: +XX (X)XX XXXX XXXX

Got it, let's break down how to build the exact regex you need. The target format has a clear, consistent structure, so we can craft a pattern that enforces every part of it—no extra characters allowed, no missing elements.

The Regex Pattern

^\+\d{1,3} \(\d\) \d{2} \d{4} \d{4}$

What Each Part Does

Let's unpack this piece by piece to make sure you understand how it enforces the format:

  • ^: Anchors the match to the start of the string (ensures nothing comes before the +)
  • \+: Matches the literal + sign (we escape it with \ because + is a special character in regex)
  • \d{1,3}: Matches 1 to 3 digits for the country code (covers common international prefixes like 1, 44, 86)
  • : Matches a single literal space (strictly enforces the space after the country code)
  • \(\d\): Matches a single digit wrapped in parentheses (like (0) in your example—\( and \) escape the literal parentheses since they're special in regex)
  • : Another single literal space, enforcing the gap after the parenthesized digit
  • \d{2}: Matches exactly 2 digits for the area code (like 20 in your example)
  • : Single space after the area code
  • \d{4}: Matches exactly 4 digits for the first number segment
  • : Single space between the two number segments
  • \d{4}$: Matches exactly 4 digits for the final segment, with $ anchoring the match to the end of the string (ensures nothing comes after this)

Example Matches

This regex will correctly match any number following the same structure, like:

  • +44 (0)20 1234 5678 (your original example)
  • +86 (0)10 9876 5432
  • +1 (1)21 5555 1234

If You Need to Adjust the Format

If you need to tweak parts of the pattern (e.g., allow more digits in the parenthesized section, or a different area code length), you can modify the quantifiers:

  • To allow 1-2 digits in the parentheses: change \(\d\) to \(\d{1,2}\)
  • To use a 3-digit area code: change \d{2} to \d{3}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 09:09:36