Regex check if backslash before every symbols using python
Question: I met some problems when I'd like to check if the input regex if correct or
not.

I'd like to check is there one backslash before every symbol, but I don't know
how to implement using Python.

For example:

  * `number: 123456789`. (return `False`)
  * `phone\:111111` (return `True`)

I try to use `(?!)` and `(?=)` in Python, but it doesn't work.

Update:

I'd like to match the following string:

`\~`, `\!`, `\@`, `\$`, `\%`, `\^`, `\&`, `\*`, `\(`, `\)`, `\{`, `\}`, `\[`,
`\]`, `\:`, `\;`, `\"`, `\'`, `\>`, `\<`, `\?`

Thank you very much.


Answer: Check that the entire string is composed of single characters or pairs of
backslash+symbol:

    
    
    import re
    def has_backslash_before_every_symbol(s):
        return re.match(r"^(\\[~!@$%^&*(){}\[\]:;"'><?]|[^~!@$%^&*(){}\[\]:;"'><?])*$", s) is not None
    

Python regex reference: <https://docs.python.org/3/library/re.html>

