What is a regular expression pattern for rule 'doesn't contain a word W'? [closed]

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

  •  29-06-2022
  •  | 
  •  

Вопрос

I want to test the web-element style: it shouldn't contain words "disabled" or "inactive".

Это было полезно?

Решение 2

Do you really need a "negative" regex? Why not create a regex that tests that it contains "disabled" or "inactive", and test for it to be false?

'status: inactive' !~ /(disabled|inactive)/

Другие советы

Using negative lookahead:

>> 'status: inactive ' =~ /(?!.*(disabled|inactive))^/
=> nil
>> 'status: disabled' =~ /(?!.*(disabled|inactive))^/
=> nil
>> 'status: enabled' =~ /(?!.*(disabled|inactive))^/
=> 0
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top