'Create regex character set from string
I have a string containing unique chars that I want to place inside regex char set. For example "abc" -> r"[abc]". I have a little bit more difficult case and tried to do something like that:
symbols = "\n \"'\\(),.!-/"
re_str = "["
for s in symbols:
re_str += s
re_str += "]"
But result isn't satisfying
[\n "\'\\(),.!-/]
The backslash before " disappeared, but the one appeared before '. Regex101 says that it is the wrong syntax for python.
Also, it doesn't work if the string contains special re chars like \d \s.
What is the best way to do such an operation?
Solution 1:[1]
The following should work:
symbols = r"\n \"'\\(),.!-/"
re_str = "[" + symbols + "]"
Tested it the following way:
print(re.match(re_str, "\n"))
print(re.match(re_str, "\""))
print(re.match(re_str, "\\"))
print(re.match(re_str, "\'"))
print(re.match(re_str, "("))
print(re.match(re_str, ")"))
...
Each of these have returned a match object with the correct span
In contrast, without the r raw string modifier, it doesn't match \\.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | tituszban |
