0

I have a problem that i would like to exclude the characters ZTzt from the list.

Currently i have a regex

(.{2,4}[^Ztzt]AB.+)

Examples that it should not match but are matching ABZ1234

Example strings that would be tested and should not be matched ABCZ1ABC ABCDZ14ABC ABZ12ABC ABDZ12ABC

aceminer
  • 3,657
  • 8
  • 42
  • 92

1 Answers1

1

Maybe you can add a separator at the beginning of the pattern.

If it can be a newline or a space, you would have the following: [\n ](.{2,4}[^Ztzt]AB.+)

As you want to account for the match of the first word (and thus it wouldn't have the separator, you could add this condition with an "or"): ([\n ]|^)(.{2,4}[^Ztzt]AB.+)

You probably want this group to not be captured, so you can indicate it: (?:[\n ]|^)(.{2,4}[^Ztzt]AB.+)

Finally, this would match ABZ12ABC as well, given that follows the pattern "2 to for characters, then something not 'Ztzt', and then AB ..." . To avoid that, you could specify that the first characters must not be among 'Ztzt': (?:[\n ]|^)([^Ztzt]{2,4}[^Ztzt]AB.+)

But we are sort of repeating the condition so we could just change the number of allowed occurrences: (?:[\n ]|^)([^Ztzt]{3,5}AB.+)

Would this do the trick? https://regex101.com/r/Lv1yQv/1

lpg
  • 4,784
  • 1
  • 13
  • 15