RE Despair - help required

Y

Yoav

I am trying the following:

re.search(r'\\[^"\\]+(?=("?$))', "c:\ret_files")

and I get a return of NoneType, and I have no idea why. I know that I
missing something here, but I really can't figure out why (I bet it's
something obvious). I also tried this RE on KODOS and it works fine
there, so I am really puzzled.

Any ideas?


Thanks.
 
R

Robert Kern

Yoav said:
I am trying the following:

re.search(r'\\[^"\\]+(?=("?$))', "c:\ret_files")

and I get a return of NoneType, and I have no idea why. I know that I
missing something here, but I really can't figure out why (I bet it's
something obvious). I also tried this RE on KODOS and it works fine
there, so I am really puzzled.

Any ideas?

Look at the second string. It has "\r" in the middle of it where you
really want "\\r" (or alternatively r"c:\ret_files").

--
Robert Kern
(e-mail address removed)

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
 
S

Sybren Stuvel

Yoav enlightened us with:
I am trying the following:

re.search(r'\\[^"\\]+(?=("?$))', "c:\ret_files")

and I get a return of NoneType, and I have no idea why.

Because you don't match a carriage return "\r".
I know that I missing something here, but I really can't figure out
why (I bet it's something obvious).

Use forward slashes instead of backward slashes. And go nag at
Microsoft for using the most widely used escape character as a path
separator...

Sybren
 
Y

Yoav

Thanks guys. Issue solved.
I am also going to give Microsoft a call about it. Any other issues you
want me to raise while I am talking to them?


Cheers.

Robert said:
Yoav said:
I am trying the following:

re.search(r'\\[^"\\]+(?=("?$))', "c:\ret_files")

and I get a return of NoneType, and I have no idea why. I know that I
missing something here, but I really can't figure out why (I bet it's
something obvious). I also tried this RE on KODOS and it works fine
there, so I am really puzzled.

Any ideas?


Look at the second string. It has "\r" in the middle of it where you
really want "\\r" (or alternatively r"c:\ret_files").
 
Y

Yoav

Don't think it will do much good. I need to get them from a file and
extract the last folder in the path. For example:
if I get "c:\dos\util"
I want to extract the string "\util"



Fredrik said:
Yoav said:
I am trying the following:

re.search(r'\\[^"\\]+(?=("?$))', "c:\ret_files")

and I get a return of NoneType, and I have no idea why. I know that I
missing something here, but I really can't figure out why


instead of struggling with weird REs, why not use Python's standard
filename manipulation library instead?

http://docs.python.org/lib/module-os.path.html

</F>
 
R

rafi

Yoav said:
Don't think it will do much good. I need to get them from a file and
extract the last folder in the path. For example:
if I get "c:\dos\util"
I want to extract the string "\util"

like frederik says (I use '/' as I am using Unix):
('c:/foo/bar', '.txt')

or if you are really reluctant:
>>> 'c:\\foo\\bar'.split ('\\') ['c:', 'foo', 'bar']
>>> 'c:\\foo\\bar'.split ('\\') [-1]
'bar'


Fredrik Lundh wrote:
instead of struggling with weird REs, why not use Python's standard
filename manipulation library instead?

http://docs.python.org/lib/module-os.path.html

</F>
 
R

Reinhold Birkenfeld

Yoav said:
Don't think it will do much good. I need to get them from a file and
extract the last folder in the path. For example:
if I get "c:\dos\util"
I want to extract the string "\util"

Then os.path.basename should be for you.

Reinhold
 
R

Robert Kern

Yoav said:
Don't think it will do much good. I need to get them from a file and
extract the last folder in the path. For example:
if I get "c:\dos\util"
I want to extract the string "\util"

You mean like this:

import os
os.path.sep + os.path.split(r"c:\dos\util")[-1]

--
Robert Kern
(e-mail address removed)

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
 
S

Sion Arrowsmith

Fredrik said:
Yoav said:
I am trying the following:

re.search(r'\\[^"\\]+(?=("?$))', "c:\ret_files")
instead of struggling with weird REs, why not use Python's standard
filename manipulation library instead?

http://docs.python.org/lib/module-os.path.html
Don't think it will do much good. I need to get them from a file and
extract the last folder in the path. For example:
if I get "c:\dos\util"
I want to extract the string "\util"

Did you actually look at the docs Fredrik pointed you at? Did you,
in particular, notice os.path.basename, which does (almost) exactly
what you want?
 
Y

Yoav

Thank you all guys. It seems like the simpler the solution, the more I
am happy about it. Sorry, for the simple question, I am quite new to
this lang.

Cheers.

Robert said:
Yoav said:
Don't think it will do much good. I need to get them from a file and
extract the last folder in the path. For example:
if I get "c:\dos\util"
I want to extract the string "\util"


You mean like this:

import os
os.path.sep + os.path.split(r"c:\dos\util")[-1]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,162
Latest member
GertrudeMa
Top