I want to validate (preferably with .matches()) that the part before % in for example “ookd&sr34!abc*%*blabla.h” does not begin or end with for example ‘k’. ‘k’ must, however, be allowed in between those endpoints (beginning to %, exclusive).
I can’t use the end anchor because I don’t want the end of the whole string. Also, I don’t want to split the string, I want to do it all in one regex.
Possibly some kind of lookahead? I’m stuck.
Something like
If you ignore the look-arounds this matches any sequence of characters up to the first %, then the %, then everything else. The lookahead at the start ensures the first character (if there is one) is not k and the look behind ensures that the last character before the first % (if there is one) is not k. Unlike the naive
[^k].*?[^k]%this allows for the case where there are fewer than two characters before the %.