Graham
Graham

Reputation: 8181

How to find matches that do not include a string in regular expressions

I'm trying to create a regular expression that will match #rn: but not if it is followed by msg.

So

#rn:abc -> matches
#rn:msh -> matches
#rn:xy -> matches
#rn:defg -> matches
#rn:msg - does not match

I have tried \#rn\:[^m][^s][^g] and \#rn\:[^(msg)] but they don't work.

Can it be done?

Upvotes: 0

Views: 46

Answers (1)

vks
vks

Reputation: 67988

\#rn\:(?!msg)

This shoudl do it for you.Here lookahead ensure that after #rn: there is not msg.See demo.

https://regex101.com/r/tX2bH4/28

Upvotes: 3

Related Questions