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

正则表达式组合匹配邮箱与指定数字,排除链接及@用户名中的数字

Solution to Combine Email & Targeted Number Detection (Excluding URL/@Mention Digits)

Got it, let's break this down and build the exact regex you need. The goal is to match emails and valid numbers (3-20 digits, with common separators) while explicitly excluding digits in URLs and @-mention usernames.

Step 1: Define Exclusion Rules First

We need to skip two types of content entirely:

  • URLs: Any string starting with http://, https://, or www.
  • @-mentions: Usernames starting with @ (e.g., @Waage1965 — we don't want the 1965 here)

Step 2: Combined Regex Pattern

Here's the merged regex that handles both email and valid number detection, with exclusions built in:

/(?!(?:https?:\/\/|www\.)[^\s]+)(?<!@\w*)(?:(([^<>()\[\]\.,;:\s@"]+(\.[^<>()\[\]\.,;:\s@"]+)*)|(".+"))@(([^<>()[\]\.,;:\s@"]+\.)+[^<>()[\]\.,;:\s@"]{2,})|(?:\+?\s?\d[-\s\/]?){3,20}\d)/gim

Let's Break Down the Key Parts:

  1. Negative Lookahead for URLs: (?!(?:https?:\/\/|www\.)[^\s]+) — Ensures the current position isn't inside a URL (matches up to the next whitespace)
  2. Negative Lookbehind for @-Mentions: (?<!@\w*) — Ensures the current position isn't immediately after an @-mention username (so digits in @Waage1965 are skipped)
  3. Email Regex: The original email pattern you provided, wrapped in a non-capturing group
  4. Valid Number Regex: (?:\+?\s?\d[-\s\/]?){3,20}\d — Matches numbers with optional + prefix, spaces, hyphens, or slashes, requiring 3-20 total digits (handles formats like +490154555356, 0175 27557996, 001-541-754-3010, 04435/1111)

Step 3: Test Against Your Sample Text

When run on your test content, the regex will match these items (exactly what you want to trigger alerts):

Emails:

  • test3@telekom.de
  • test2@gmx.net
  • test1@t-online.de
  • siegrun@telekom.de

Valid Numbers:

  • 215685553555
  • +490154555356
  • 0175 27557996
  • 0175 12345678
  • 001-541-754-3010
  • 04435/1111
  • 12334546546565
  • 04435/1111

It will not match:

  • Digits in URLs (like 3055244 or 28054 in the links)
  • Digits in @-mentions (like 1965 in @Waage1965)

Notes for Implementation

  • Use the gim modifiers: g for global matches, i for case-insensitive email matching, m for multi-line support
  • If you need to capture emails and numbers separately, you can use capture groups (the email is in group 1, numbers in the unnamed group)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 03:52:00