Issue
I'm trying to create a regex, that matches only symbols (no letters, no digits) and that ignores dots and new lines.
I'm failry new to regexes an strugling. \D\W
should work letter and digit wise but I can't find out how to NOT match something else. I'm using the re module in python 3.11. (Yes I'm trying to do advent of code day 3)
Solution
You can use the pattern below.
Pattern
[^\w\n\s.]
This blocks all word characters (\w
), new lines (\n
), and dots (.
).
The caret ^
is used to negate the patterns inside the square brackets
Test
import re
pattern = re.compile(r'[^\w\n\s.]')
result = pattern.findall("Test me")
print(result)
It will output an empty list []
.
Answered By - Ken Verdadero
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.