Page 1 of 1

RegEx for may not start with...

Posted: Thu Jul 03, 2008 3:22 am
by phy8ig
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

Re: RegEx for may not start with...

Posted: Fri Jul 04, 2008 4:31 am
by prometheuzz
phy8ig wrote:...
for example ^[^(GS_)] matches strings that do not start with GS_, but it also matches an empty string.

Ian
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].

The following regex matches any non-empty string that does not start with "GS_":

Code: Select all

'/^(?!GS_).+/'
Read it like this:
- 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