正则表达式组合匹配邮箱与指定数字,排除链接及@用户名中的数字
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://, orwww. - @-mentions: Usernames starting with
@(e.g.,@Waage1965— we don't want the1965here)
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:
- Negative Lookahead for URLs:
(?!(?:https?:\/\/|www\.)[^\s]+)— Ensures the current position isn't inside a URL (matches up to the next whitespace) - Negative Lookbehind for @-Mentions:
(?<!@\w*)— Ensures the current position isn't immediately after an @-mention username (so digits in@Waage1965are skipped) - Email Regex: The original email pattern you provided, wrapped in a non-capturing group
- 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.detest2@gmx.nettest1@t-online.desiegrun@telekom.de
Valid Numbers:
215685553555+4901545553560175 275579960175 12345678001-541-754-301004435/11111233454654656504435/1111
It will not match:
- Digits in URLs (like
3055244or28054in the links) - Digits in @-mentions (like
1965in@Waage1965)
Notes for Implementation
- Use the
gimmodifiers:gfor global matches,ifor case-insensitive email matching,mfor 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
相关产品推荐
相关产品推荐

