Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # re_del_non_alphanum.py
- import re
- # Sample string
- text = "Example (123) 321 - okay - [456.789] - (abc) python [def] - 12345 - 67890"
- # Correct regex pattern using alternations for each start/end pair
- pattern = r'\([^a-zA-Z]*\)|\[[^a-zA-Z]*\]|-[^a-zA-Z]*(?:-|$)'
- # Replace the matched substrings with an empty string
- result = re.sub(pattern, '', text)
- # Remove any extra spaces left by removals
- result = re.sub(r'\s+', ' ', result).strip()
- print(result)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement