Question regarding Python regex parsing structures #198031
Replies: 3 comments
-
|
You can solve this by using regex with negative lookbehind to exclude patterns that match your system line serial IDs while still capturing phone numbers. Here's a working solution: Regex Pattern: (?<!\bSERIAL[-\s]?\d{4,}\b)(\+\d{1,3}[-.\s]?\(?\d{1,4}\)?[-.\s]?\d{3,4}[-.\s]?\d{3,4}\b)How it works: · (?<!\bSERIAL[-\s]?\d{4,}\b) → Negative lookbehind that skips any "SERIAL" followed by digits Example in Python: import re
text = """System line: SERIAL123456
Customer contact: +1-555-123-4567
Another serial: SERIAL-789012
User phone: (212) 555-7890"""
phone_pattern = r'(?<!\bSERIAL[-\s]?\d{4,}\b)(\+\d{1,3}[-.\s]?\(?\d{1,4}\)?[-.\s]?\d{3,4}[-.\s]?\d{3,4}\b)'
masked = re.sub(phone_pattern, r'[PHONE MASKED]', text)
print(masked)
Output:
System line: SERIAL123456
Customer contact: [PHONE MASKED]
Another serial: SERIAL-789012
User phone: [PHONE MASKED] |
Beta Was this translation helpful? Give feedback.
This comment was marked as spam.
This comment was marked as spam.
-
|
Thanks for posting in the GitHub Community, @harshith-aa! We’ve moved your post to our Programming Help 🧑💻 category, which is more appropriate for this type of discussion. Please review our guidelines about the Programming Help category for more information. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
🏷️ Discussion Type
Question
💬 Feature/Topic Area
Code Search and Navigation
Body
What is the most efficient way to use regex to mask phone numbers without matching system line serial IDs?
Beta Was this translation helpful? Give feedback.
All reactions