c# - Multiple RegEx negation matching -
i have following regex patterns:
"[0-9]{4,5}\.fu|[0-9]{4,5}\.ng|[0-9]{4,5}\.sp|[0-9]{4,5}\.t|jgb[a-z][0-9]|jni[a-z][0-9]|jn4f[a-z][0-9]|jnm[a-z][0-9]|jti[a-z][0-9]|jtm[a-z][0-9]|niy[a-z][0-9]|ssi[a-z][0-9]|jni[a-z][0-9]-[a-z][0-9]|jti[a-z][0-9]-[a-z][0-9]"
===> matches 8411.t or jnid8"[0-9]{4,5}\.hk|hsi[a-z][0-9]|hmh[a-z][0-9]|hcei[a-z][0-9]|hcei[a-z][0-9]-[a-z][0-9]"
==> matches 9345.hk or hceiu9-a9".*\.si|sfc[a-z][0-9]"
==> matches 8345.si or sfcx8
how can obtain regex negation of these patterns? want match strings match neither of these 3 patterns: e.g. want match 8411.abc, not of aforementioned strings (8411.t, hceiu-a9, 8345.si, etc.).
i've tried (just exclude 2 , 3 instance, doesn't work):
^(?!((.*\.si|sfc[a-z][0-9])|([0-9]{4,5}\.hk|hsi[a-z][0-9]|hmh[a-z][0-9]|hcei[a-z][0-9]|hcei[a-z][0-9]-[a-z][0-9])))
the main idea here place patterns (?!.*<pattern>)
negative lookaheads anchored @ start of string (^
). difficulty here patterns contain unanchored alternations, , if not grouped, .*
before patterns refer first alternative (i.e. subsequent alternatives negated @ start of string.
thus, pattern formula ^(?!.*(?:<pattern1>))(?!.*(?:<pattern2>))(?!.*(?:<pattern3>))
. note .+
or .*
@ end optional if need boolean result. note in last pattern, need remove .*
in first alternative, won't make sense use .*.*
.
use
^(?!.*(?:[0-9]{4,5}\.fu|[0-9]{4,5}\.ng|[0-9]{4,5}\.sp|[0-9]{4,5}\.t|jgb[a-z][0-9]|jni[a-z][0-9]|jn4f[a-z][0-9]|jnm[a-z][0-9]|jti[a-z][0-9]|jtm[a-z][0-9]|niy[a-z][0-9]|ssi[a-z][0-9]|jni[a-z][0-9]-[a-z][0-9]|jti[a-z][0-9]-[a-z][0-9]))(?!.*(?:[0-9]{4,5}\.hk|hsi[a-z][0-9]|hmh[a-z][0-9]|hcei[a-z][0-9]|hcei[a-z][0-9]-[a-z][0-9]))(?!.*(?:\.si|sfc[a-z][0-9])).+
see regex demo.
you may contract formula ^(?!.*(?:<pattern1>|<pattern2>|<pattern3>))
:
^(?!.*(?:[0-9]{4,5}\.fu|[0-9]{4,5}\.ng|[0-9]{4,5}\.sp|[0-9]{4,5}\.t|jgb[a-z][0-9]|jni[a-z][0-9]|jn4f[a-z][0-9]|jnm[a-z][0-9]|jti[a-z][0-9]|jtm[a-z][0-9]|niy[a-z][0-9]|ssi[a-z][0-9]|jni[a-z][0-9]-[a-z][0-9]|jti[a-z][0-9]-[a-z][0-9]|[0-9]{4,5}\.hk|hsi[a-z][0-9]|hmh[a-z][0-9]|hcei[a-z][0-9]|hcei[a-z][0-9]-[a-z][0-9]|\.si|sfc[a-z][0-9])).+
see another regex demo.
wiki
Comments
Post a Comment