Advertisement
here2share

# re_del_non_alphanum.py

Jan 26th, 2025 (edited)
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.46 KB | None | 0 0
  1. # re_del_non_alphanum.py
  2.  
  3. import re
  4.  
  5. # Sample string
  6. text = "Example (123) 321 - okay - [456.789] - (abc) python [def] - 12345 - 67890"
  7.  
  8. # Correct regex pattern using alternations for each start/end pair
  9. pattern = r'\([^a-zA-Z]*\)|\[[^a-zA-Z]*\]|-[^a-zA-Z]*(?:-|$)'
  10.  
  11. # Replace the matched substrings with an empty string
  12. result = re.sub(pattern, '', text)
  13.  
  14. # Remove any extra spaces left by removals
  15. result = re.sub(r'\s+', ' ', result).strip()
  16.  
  17. print(result)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement