Hi
Have really struggled to find a regex to identify strings which may start with, f. eks. "FBI_", but are not an empty string.
for example ^[^(GS_)] matches strings that do not start with GS_, but it also matches an empty string.
Ian
RegEx for may not start with...
Moderator: General Moderators
- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: RegEx for may not start with...
No, that matches a string that does not start with any of the characters '(', 'G', 'S', '_' or ')' since you put the characters in a "character class" (aka a "character set") [1].phy8ig wrote:...
for example ^[^(GS_)] matches strings that do not start with GS_, but it also matches an empty string.
Ian
The following regex matches any non-empty string that does not start with "GS_":
Code: Select all
'/^(?!GS_).+/'- the start of the string '^'
- not followed by 'GS_'
- followed by one or more characters of any type '.+'
The (?!...) is called "negative look ahead" [2].
HTH & good luck.
[1] http://www.regular-expressions.info/charclass.html
[2] http://www.regular-expressions.info/lookaround.html