Home>
from Re Import *
V= Split (R "[\ s,) (]", R "(C: \ Users \ Gleb \ Desktop \ Sites.txt, C: \ Users \ Gleb \ Desktop \ iStock_000020116885Large _-_ Copy-700x461.jpg)")
Print (V)

Code displays:

['', 'C: \ Users \ Gleb \ Desktop \ Sites.txt', 'C: \ Users \ Gleb \ Desktop \ iStock_000020116885Large _-_ Copy-700x461.jpg', '']

and should output:

['C: \ Users \ Gleb \ Desktop \ Sites.txt', 'C: \ Users \ Gleb \ Desktop \ iStock_000020116885Large _-_ Copy-700x461.jpg']

Why does it displays?

Strip () Apply initially to the string, or.

Visman2021-04-09 03:44:16
  • Answer # 1

    You cut on the specified literals, this is exactly what Python makes , on the one hand literal C. and on the other? Nothing is an empty string, so he inserts it.

    What would you get what you need, you have to say Piton What do you want to get , and not that you do not need .

    In the comments @ Gil9RED offered a more correct regular expression: R "\ w. +? \. \ W +"

    Import Re
    String= R "(C: \ Users \ Gleb \ Desktop \ Sites.txt, C: \ Users \ Gleb \ Desktop \ iStock_000020116885Large _-_ Copy-700x461.jpg)"
    Regex= R "([\ w \: \\\. \ -] +)"
    Print (Re.Findall (Regex, String))
    

    Live example

    I got such a regular: R "\ w:. +? \. \ W +". Example: regex101.com/r/pxsqx3/1.

    gil9red2021-04-09 03:44:16

    @ Gil9RED, your even better, with a small reservation "\ w. +? \. \ w +" The colon is not needed. Add it in response.

    Vasiliy Rusin2021-04-09 03:44:16

    The colon indicated for the letter of the disk C :, D:

    gil9red2021-04-09 03:44:16

    @ GIL9RED You already specify. +?. The colon is excessful here, and in any case you do not shield it, it does not do what you expect.

    Vasiliy Rusin2021-04-09 03:44:16

    I wrote regularly guided by what I want to find ways of files from beginning to end, the beginning is the letter of the disk (\ w :), after I am looking for all the characters (. +?) To the end of the file, on its suffix (\. \ W +). Therefore, I do not think that there is redundancy. It is clear that the suffix is ​​so possible in the name folder to meet, but the restrictions should be. In general, about redundancy, the same result can be achieved even easier. I will show in response

    gil9red2021-04-09 03:44:16
  • Answer # 2

    In simple cases of text processing, you can do without regularly:

    Text= R (C: \ Users \ Gleb \ Desktop \ Sites.txt, C: \ Users \ Gleb \ Desktop \ iStock_000020116885Large _-_ Copy-700x461.jpg) "
    FileName_List= Text [1: -1] .split (',')
    Print (FileName_List)
    

    Result:

    ['C: \\ Users \\ Gleb \\ Desktop \\ Sites.txt', 'C: \\ Users \\ Gleb \\ Desktop \\ iStock_000020116885Large _-_ Copy-700x461.jpg']