Regular expression for spaces but not escaped spaces

I am parsing the input of ls -lb and trying to put each field in an array in PHP.

My input looks something like this:

-rwx------  1 user.name users   546879 2008-09-05 09:23 Newspaper_Templates.pdf
-rwx------  1 user.name users   403968 2009-01-12 14:16 P2_05.ppt
-rwx------  1 user.name users   144896 2009-01-12 14:08 P2.5_Using_CRO.ppt
drwx------  8 user.name users     4096 2009-09-15 10:21 School\ 09-10
drwx------  2 user.name users     4096 2010-06-28 13:59 SMART\ Notebook
drwx------  2 user.name users     4096 2009-11-30 13:35 Templates

I look at each line at a time and try to replace several spaces with one space, except for those that are escaped with.

Each line is read into a variable called $ filedata, and then through it:

    $filedata = preg_replace("/\s+/", " ", $filedata);
    print_r (preg_split("/[\s]/", $filedata));

This almost works, but for lines with escaped spaces, obviously not. How can I change this so that my split works for spaces, but not escaped spaces?

(Alternatively, is there a better way? If I could get ls to give me a list with each field separated by a comma or something else, that would be even better!)

+3
3

, , preg, :

$filedata = preg_replace("/(?<!\\\\)\s+/", " ", $filedata);
print_r(preg_split("/(?<!\\\\)\s/", $filedata));

(?<!\\\\) PHP (?<!\\), , \

+1

.

PHP stat.

+1

Try with (?<!\\)\s+

0
source

Source: https://habr.com/ru/post/1764032/


All Articles