¿Hay alguna expresión regular en Python para averiguar el espacio en la ruta de la carpeta?

I have a python strings as below:

mystring1 = "command1 "  "D:\\The palace\\The King\\ " "D:\\The palace\\The Queen\\"

mystring2 = "command2 "  "D:\\Thepalace\\TheKing\\ " "D:\\Thepalace\\TheQueen\\"

Is there any regular expression by which we can find out whether a space exists in the folder path

ie how can i distinguish mystring1 a mystring2 by using a regular expression

preguntado el 31 de julio de 12 a las 15:07

Those don't appear to be valid python strings, did you mean mystring1="command1 D:\The place\The King\ D:\The place\The Queen\" o quizás mystring1='"command1 "D:\The place\The King\" "D:\The place\The Queen\"' -

...see you edited it, but still not valid strings. We should be able to copy and paste these code snippets into a python prompt, and not get a syntax error. Fix them. -

You don't need a regex. A simple equality check will fail (after you properly escape the backslashes). -

"Is there any regular expression by which we can find out whether a space exists in the folder path." Sure... \S* matches any non-whitespace character. If there is a space, it won't match. -

3 Respuestas

Siempre que mystring1 y mystring2 will be proper Python strings, I think you want to take a look at the shlex Python package. It's specifically created for parsing such strings. Then you can use shlex.get_token() to retrieve the command, and the arguments to the command, quotes/spaced parsed and all.

Respondido 31 Jul 12, 15:07

You may not need regular expression indeed. A simple string method string.find(string) haría.

s = "some string"
space = " "
s.find(space) # gives -1 when space is not in s, otherwise the offset of its 1st appearance

EDITADO:

This answer is edited according to the edition of the question.

Since you're on Windows, I present you ":\\. .*\\" for commands with absolute paths. And as for relative paths, I'm afraid there won't be a nice solution to detect.

Respondido 31 Jul 12, 15:07

This will not work if the string contains multiple paths as suggested by the question. See my answer for a more complete solution. - jmetz

@mutzmatron Yes that's true, and I've made an edition. Actually when I gave that answer, the strings given by him were invalid for Python. So forgive me, dude. - estrellar

Your addition still doesn't solve the problem. See my answer. - jmetz

Suponiendo que quisiste decir

mystring1="command1 D:\\The place\\The King\\ D:\\The place\\The Queen\\"

You could split the string at occurrences of D:\\ and then removing spaces from the substrings will give you space free paths, e.g.

subs = mystring1.split('D:\\')
mystring2 = subs[0]
for s in subs[1:]: 
    mystring2 += ' D:\\' + s.replace(' ', '')

For example I applied this and got

>> mystring2
    'command1  D:\\Theplace\\TheKing\\  D:\\Theplace\\TheQueen\\'

If you just need to distinguish them use

def has_spaces(str1):
    subs = str1.split('D:\\')
    for s in subs[1:]: 
        if s.strip().count(' ') > 0:
        return True

>> has_spaces(mystring1)
    True
>> has_spaces(mystring2)
    False

to detect if any of the paths contain spaces. Cheers to Pengyu CHEN for pointing out the OP asked an easier question.

Respondido 31 Jul 12, 15:07

What he want is to distinguish those two strings, but not to transform one to another. - estrellar

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.