vharekal
vharekal

Reputation: 165

Negate a RegEx when using OR `|`

I have a RegEx pattern

^(\d+|\w+-\d+)$

How do I match all those strings which do not match this pattern?

Upvotes: 2

Views: 667

Answers (1)

Sahil Gulati
Sahil Gulati

Reputation: 15141

If you look into it and try to understand that. Logically it will be like this. Lets say if have a condition which say either X or Y then negation of its will be neither X and nor Y.

X or Y negation will be equal to Neither X and Nor Y.

For that you can try this.

Regex code demo

Regex: ^(?!\d+$)(?!\w+-\d+$).*$

1. ^ start of string.

2. (?!\d+$) negative look-ahead for digits till the end of string.

3. (?!\w+-\d+$) negative look-ahead for words then - and then digits till the end.

4. .*$ match all till then end.

Upvotes: 3

Related Questions