I’m trying to parse a string with regex. I need to get the first part before a clean &. A clean & does not have + right before or after it.
Clean &:
- 47&78
- a+bc&gsd+f
Not Clean &:
- fA+&71
- nap&+sys
For Matisse+&+Sadko&sendtemp=1 the regex should return Matisse+&+Sadko.
I’ve tried to do it that way, but I it doesn’t work:
.*(?!\+)&(?!\+).*
Thanks.
The
(?!...)is a (negative) lookahead expression, it checks whether the next characters do (not) match. So, you would need to add that one character to your regex, because the&is never a+:But that would need at least one character before the
&. Better use (negative) lookbehind:Read more on Regex Lookaround.
If your
&is never at the string beginning or end, you could also just use a negated character class:…or, if it could, let that be optional: