What is the shortest regular expression that accepts anything except for the three characters: |, ^ and ~ (a pipe, a caret and a tilde)?

StackOverflow https://stackoverflow.com/questions/4293358

  •  28-09-2019
  •  | 
  •  

Question

I have a regex but it is very big. I specify the set of chars that I allow. That makes it big regex expression. Will it be simpler if I can specify the opposite i.e. just specify what chars I won't accept?

^[^\|\^~]*$

But it is not working. Any clue?

Was it helpful?

Solution

This matches strings that do not contain those three characters anywhere:

^[^|^~]*$

While this matches all strings that contain any of those three anywhere:

[|^~]

The two patterns are equivalent, so you could either use the first one, or use the second one with negation.

OTHER TIPS

You mustn't escape | or ~ within []. Use ^[^|^~]*$.

>>> re.match(r'^[^|^~]*$', 'abc')
<_sre.SRE_Match object at 0x2dbc4a8>
>>> re.match(r'^[^|^~]*$', '^')
>>> re.match(r'^[^|^~]*$', '|')
>>> re.match(r'^[^|^~]*$', '~')

i.e. the first example matches, the other three fail as required.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top