A regular expression to match spaces or spaces with the prefix '\\'

I have a list of file names separated by spaces, where spaces in file names have the prefix '\'

eg. "first \ file second \ file"

How can I make my regular expression match every file name?

+3
source share
3 answers
(\\ |[^ ])+

Everything except spaces, unless they are escaped. Should work, sorry for not understanding your question initially.

+14
source
(\S|(?<=\\) )+

Explanation:

You are looking for multiple characters without a space ( \S) or a space preceded by a backslash several times.

mach 1, , .

, . , (?: , ):

(?:\S|(?<=\\) )+
+5

:

/[^ \\]*(?:\\ [^\\ ]*)*/

Friedl " ". , , , , . , , .

: (Tomalak) , , , . , , ( ) .

(Brad) OP , , . , , . :

/"[^\\"]*(?:\\.[^\\"]*)*"/

(Tomalak) I do not know what you mean when you say that it does not match the "file name at the beginning of the line." They seem to match the file names in the OP example. However, it also matches an empty string, which is not very good. This can be fixed, but if efficiency is not a problem, it is not worth the effort. Stefan's solution works great.

+1
source

All Articles