1

I will give an example, I have two strings:

FL_0DS906555B_3661_27012221225012_V001_S

FL_0DS906555C_3661_27012221225012_V001_S

And I want to get any string, that has no "0DS906555B" in it, has "2701222122" in it and "5012" is in range of 5003-5012.

My regex looks like this:

^.*(?!.*0DS906555B).{6}2701222122(500[3-9]|501[0-2]).*$

unfortunately it keeps matching everything all the time. I have looked into many posts here but nothing helped for me since people usually asked for less complex, smaller strings.

Thank you

  • Did you look at https://stackoverflow.com/questions/406230/regular-expression-to-match-a-line-that-doesnt-contain-a-word/406408#406408 ? – Barmar Oct 27 '22 at 17:26
  • Do all the strings have the same length and the numbers are in the same position? Maybe you could use `in`, `not in` instead of regex. – Ignatius Reilly Oct 27 '22 at 17:28
  • did look into that post, that is where I first learned about negative lookahead, but I could've missed something in the comments since that post is really big. Yes, all the strings should be the same length with the same positions. The 'in' or 'not in' could be an option but because of the tool I have to do it with it, the regex is more suitable unfortunately. – Michal Pivarc Oct 27 '22 at 17:41
  • You need to [put the lookahead in the start](https://regex101.com/r/gPRfbZ/1), else the condition will still succeed at any point within/after the substring that you want to disallow. – bobble bubble Oct 27 '22 at 18:20

1 Answers1

1

Try (regex101):

^(?!.*0DS906555B)(?=.*_2701222122(?:500[3-9]|501[012])_).*$
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
  • 1
    Wow, thank you! I played with it and it looks like the main part that fixed my problem was with the `.*` before `27012..` and you made it so there is no grouping, very nice :). – Michal Pivarc Oct 28 '22 at 00:19