RegEx for may not start with...

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
phy8ig
Forum Newbie
Posts: 1
Joined: Thu Jul 03, 2008 3:12 am

RegEx for may not start with...

Post 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
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: RegEx for may not start with...

Post 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
Post Reply