Page 1 of 1

Regex to detect filesystem path values

Posted: Tue Jul 31, 2007 9:19 am
by harsha
I constructed the following regex to detect filesystem path values.
these path values are randomly present in a log file and they are embedded inside single quotes.

some thing like this:
this is the project path: '/usr/share/project/'


Assumptions:

No special chars will be there in the filesystem path that is: (~!@#$%^&*():;''><.,/?)

Code: Select all

\'(\/+[\w-_.\s]{1,255})+\'

comments Please, so that I can optimize further.

Posted: Tue Jul 31, 2007 10:52 am
by superdezign
\w can allow "special characters."

And what if the path doesn't start with a slash?

Posted: Tue Jul 31, 2007 11:39 am
by stereofrog
I'd suggest

Code: Select all

$path = "~
   /? # optional root
   ([\w.-]+/)* # 0 or more directory names, followed by slash
   ([\w.-]*) # possibly empty filename
~x";