tags:

views:

44

answers:

2

I'm not sure if I've phrased it correctly, but hopefully the example will clear it up:

re.search(fileMask.replace('*','.*?'),fileName):

For the first parameter in the re.search() call, how can I ensure that I will pass the value returned by the fileMask.replace() call as a raw string?

Something to the effect of:

re.search(r'fileMask.replace('*','.*?')',fileName):

..although that won't work because I actually need the fileMask function to be called.

+1  A: 
re.search(fileMask.replace('\*','\.\*?').encode('string_escape'), fileName):
MikeWyatt
As long as r'some text' is equivilant to 'some text'.encode('string_escape'), then this is exactly what I was looking for. Thanks.
rmisio
It is. `'\r\n' == r'\r\n'` returns False, and `'\r\n'.encode('string_escape') == r'\r\n'` returns True.
MikeWyatt
+2  A: 

There is no such type as "a raw string" -- there are literals (of string types) that are so named, but the objects such literals stand for are string objects -- nothing more, nothing less. For example, literals r'a\b'' (a "raw string literal") and 'a\\b' (a normal string literal) represent exactly the same string value: one of length three, with characters a, backlash, and b, in this order. If you print these objects, both display as a\b; if you print their repr, it's a\\b in both cases.

So, it's hard to understand your question. Could you give examples of some possible values for fileMask and fileName, and the results you expect from the consequent re.search calls?

Also,

I actually need the fileMask function to be called.

That might really be a problem, because there appears to be no function named fileMask, but rather (it would seem) a string thus named. Do you mean "need the method of fileMask to be called"?

Alex Martelli
I meant that I need the replace method called of the string variable fileMask. To put my question another way:myFunctionA(myFunctionB()) - Without modifying myFunctionB(), how can I ensure the return value of myFunctionB is passed as a "raw string" to myFunctionA in the above statement?
rmisio
@rmisio, there **is** no such thing as `passed as a "raw string"`: reread my answer. A raw string LITERAL (there is no other kind of "raw string"!) is just a slightly different SYNTAX to express a perfectly normal string at compile time; there just is no such thing as "passing as" (at runtime) any specific alternative form of compile-time *syntax* -- seriously, your repeated assertion to the contrary makes as much sense as the famous "colorless green ideas sleep furiously". **PLEASE** edit your Q to provide **examples**, as I already asked you to do.
Alex Martelli