1

I'm trying to write a regex for golang that matches a string that doesn't end with @abcdefghijk.com. Is this possible? Golang doesn't support negative lookaheads.

Basically the equivalent of: ^(?!.*@abcdefghijk\.com$)

  • @wiktor-stribiżew, I'm afraid, your closing of this answer is a bit premature, or at least it should not be closed with _that_ answer as a dumplicate ;-) The chief problem is that the stock Go's RE engine does not support negative lookahead, as detailed, say, [there](https://stackoverflow.com/a/26792316/720999). – kostix Aug 22 '23 at 18:00
  • 1
    Mitchel, are you sure you cannot work that around by testing your strings with `!strings.HasSuffix()`? Go's REs do not support negative lookahead for a reason, so it may be better to not rely on REs for such a trivial case in the first place. To quote Jamie Zawinski, «Some people, when confronted with a problem, think „I know, I'll use regular expressions.” Now they have two problems.» ;-) – kostix Aug 22 '23 at 18:02
  • Go’s regexes are not complete. The easiest solution I found was using the package here: https://pkg.go.dev/github.com/dlclark/regexp2. I also agree that this question shouldn’t have been closed as a duplicate. – SupaMaggie70 b Aug 22 '23 at 18:52
  • "Go’s regexes are not complete." Care to elaborate? Which regular language isn't matched by Go's regular expressions? – Volker Aug 22 '23 at 19:20
  • @SupaMaggie70b "[N]ot complete" according to whom? The same person that says PCRE and .NET are "complete", perhaps? – InSync Aug 22 '23 at 19:22
  • 1
    If you can use capturing groups, try a variation of [The Trick](https://www.rexegg.com/regex-best-trick.html#thetrick): [`^.*@abcdefghijk\.com$|(match this)`](https://regex101.com/r/hOlxfY/1) (tried [a golang demo](https://tio.run/##nY7BCsIwEETv@YolKiYiAa@CIAjeBMGjVZqmaRo1SUlTEdRvr7UV6lX3tOzOvBnl6rrg4syVBMO1RUibwvkABEEzODMBd5uXSt4KjChCWWVFqyYU7u3Xw3wBnYJtqjKsnCn0RZL4yCZLnohUZirXp3PEhDPDBzE8iBxCrksa05bQBLGt1zZkBI8G18jiKXi21jbdheasdlXSmkjce2O6nx1@tafOjgP0kO9673Z/QZPqG/lBPOv6BQ) at tio.run) - Else see [this question](https://stackoverflow.com/q/1687620/5527985) – bobble bubble Aug 23 '23 at 11:23

1 Answers1

0

A regex not ending with @abcdefghijk.com for POSIX is

^(.*([^@].{15}|.[^a].{14}|.{2}[^b].{13}|.{3}[^c].{12}|.{4}[^d].{11}|.{5}[^e].{10}|.{6}[^f]).{9}|.{7}[^g].{8}|.{8}[^h].{7}|.{9}[^i].{6}|.{10}[^j].{5}|.{11}[^k].{4}|.{12}[^.].{3}|.{13}[^c].{2}|.{14}[^o].|.{15}[^m]|.{0,15})$

See Regex: match everything but a specific pattern for details (scroll to "a string ending with a specific pattern" and see the POSIX workaround).

See the regex demo.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563