Regular expression. Is it possible to match (a or b) and (a or c) and (a or d), but not aaa?

I'm trying to censor a dirty word. Users can replace some characters with *.

Let's take the word “dog” for example (this is certainly not dirty)

  • * og
  • d * g
  • do *
  • d **

etc.

Regex looks like (d|\*)(o|\*)(g|\*)

Is it possible to combine all the options above, but not "***"?

+4
source share
3 answers

Yes, Python has negative statements. Do you want to combine [d*][o*][g*]and (?!\*{3}):

(?!\*{3})[d*][o*][g*]
+4
source
^(?!\**$)[dog*]+$

You can try something like this using lookahead. Watch the demo.

https://regex101.com/r/hE4jH0/23

+1
source

:

^\w{0,3}\*{0,2}\w{0,3}(?!\*)
0
source

Source: https://habr.com/ru/post/1618251/


All Articles