How to rewrite the regex ^(?!master).+@
without the negative lookahead?
Asked
Active
Viewed 694 times
0

Natalie Perret
- 8,013
- 12
- 66
- 129
-
What exactly are you trying to match? – AleksW Apr 15 '19 at 13:01
-
1Why do you not want the negative lookahead? – Sweeper Apr 15 '19 at 13:01
-
@Sweeper not longer supported in the engine I am forced to use... – Natalie Perret Apr 15 '19 at 13:03
-
@AleksW anything that does not start with `master` followed by anything with an `@` – Natalie Perret Apr 15 '19 at 13:04
-
1@ndnenkov sure, actually it's the kind of regex that can be accepted in the gitlab-ci.yml, it used to be the case that regex worked, but since the last update, everything is broken. – Natalie Perret Apr 15 '19 at 13:12
2 Answers
3
^(?:[^m]|m[^a]|ma[^s]|mas[^t]|mast[^e]|maste[^r]).*@
Update - in case you need one for both master
and main
:
^(?:[^m]|m[^a]|ma[^si]|mai[^n]|mas[^t]|mast[^e]|maste[^r]).*@

ndnenkov
- 35,425
- 9
- 72
- 104
-
not supposed to have a `+` instead of `*` at the end: `^(?:[^m]|m[^a]|ma[^s]|mas[^t]|mast[^e]|maste[^r]).+@`? – Natalie Perret Apr 15 '19 at 13:24
-
2@EhouarnPerret the `+` means "one or more characters", while `*` means "zero or more characters". Given that we already matched at least one character in the brackets (for example `[^m]` is exactly one character), we may or may not need another one. Hence why it's `*` and not `+`. – ndnenkov Apr 15 '19 at 13:27
-
-
-
-
-
1
-
@ndnenkov I tried but it didn't work, at least when I tested it on regex101 ... Do you have a working example ? – TBG Dec 21 '22 at 17:23
-
@TBG well what didn't work exactly? Could you post a regex so that I could suggest a fix for it. – ndnenkov Dec 21 '22 at 19:17
-
@ndnenkov I started from your answer https://regex101.com/r/GYX742/1 and I modified it to also avoid main, here is one of my tries : https://regex101.com/r/dW7CzD/1 – TBG Dec 22 '22 at 11:22
-
1@TBG here's an [updated working version](https://regex101.com/r/j03d9p/1). – ndnenkov Dec 22 '22 at 11:58
-
@ndnenkov Awesome, thanks a lot ! However I supposed there is no way of avoiding `mastere` or `mainu` being excluded ? And how would you generate that for a list of words ? I had a bash script but in fact it only works for one word, as I built it on your first answer... I think I will end up doing a loop on a "simple" regex, instead of finding a regex that covers all cases/words – TBG Dec 22 '22 at 15:26
-
1@TBG the original question required stuff starting with `master` to be excluded, not `master` alone. So `mastere` and `mainu` change the scope of the question. Ask a separate one. And yes, if you have control over it (like it's a bash script) I wouldn't build a mega-regex and match against a list of words. Instead I would check one by one without regexes. xd – ndnenkov Dec 22 '22 at 16:14
2
You may phrase this problem as being logically equivalent to saying match any string which does not begin with master
, and which contains an at symbol:
input = "some random text @";
if (input !~ /^master/ && input =~ /.*@.*/) # or /.*@$/ if it must end in an @
puts "MATCH"
end

Tim Biegeleisen
- 502,043
- 27
- 286
- 360