views:

25

answers:

2

Hi,

I'm trying to validate a system path. This path is valid if it begins with some dirs and don't contain two dots one another.

#valid path
/home/user/somedir/somedir_2
#evil path
/home/user/../somedir_2/

I know how to check for two dots in a path:

\.\. or [\.]{2}

But I really want to do something like that:

/home/user/<match everything but two dots>/somedir_2/

so that "match everything but two dots" can be everything but two dots.

I have tried:

/home/user/[^\.{2}]*/somedir_2/

but without any success.

A: 

The specification isn't clear, but you can use negative lookahead to do something like this:

^(?!.*thatPattern)thisPattern$

The above would match strings that matches thisPattern, but not if it contains a match of thatPattern.

Here's an example (as seen on rubular.com):

^(?!.*aa)[a-z]*$

This would match ^[a-z]*$, but not if it contains aa anywhere.

References

polygenelubricants
A: 
^/home/user/(?!.*\.\.).*$

will match your good pattern and reject your evil one.

Amadan