468,170 Members | 1,905 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,170 developers. It's quick & easy.

Cleaning strings with Regular Expressions

Hello,

I often find myself cleaning up strings like the following:

setAttr ".ftn" -type "string" /assets/chars/
/boya/geo/textures/lod1/ppbhat.tga";

Using regular expressions, the best I can do so far is using the re.sub
command but it still takes two lines. Can I do this in one line? Or
should I be approaching this differently? All I want to end up with is
the file name "ppbhat.tga".

Python Code:
lines[indexC]=re.sub("[\s,\S,]*/", "", lines[indexC])
lines[indexC]=re.sub(".tga[\s,\S,]*", ".tga", lines[indexC])

Thanks for your time,

/\/\ason S

Sep 6 '05 #1
6 2211
"sheffdog" <ma************@gmail.com> wrote:
setAttr ".ftn" -type "string" /assets/chars/
/boya/geo/textures/lod1/ppbhat.tga"; Can I do this in one line?

os.path.basename("/assets/chars/.../lod1/ppbhat.tga")

'ppbhat.tga'

</F>

Sep 6 '05 #2
May not be what you are looking for, but this works:

import os
s='setAttr ".ftn" -type "string" ' \
'/assets/chars/boya/geo/textures/lod1/ppbhat.tga";'
fname=os.path.basename(s.split()[-1])
BTW-It does depend on the file/path being the last item
on the line.

Larry Bates
sheffdog wrote:
Hello,

I often find myself cleaning up strings like the following:

setAttr ".ftn" -type "string" /assets/chars/
/boya/geo/textures/lod1/ppbhat.tga";

Using regular expressions, the best I can do so far is using the re.sub
command but it still takes two lines. Can I do this in one line? Or
should I be approaching this differently? All I want to end up with is
the file name "ppbhat.tga".

Python Code:
lines[indexC]=re.sub("[\s,\S,]*/", "", lines[indexC])
lines[indexC]=re.sub(".tga[\s,\S,]*", ".tga", lines[indexC])

Thanks for your time,

/\/\ason S

Sep 6 '05 #3
Using basename works, but leaves the extra stuff at the end.
Which would have to be removed with another line of code

I get this--> ppbhat.tga";

Thanks, for the idea though.
/\/\ason

Sep 6 '05 #4
"sheffdog" <ma************@gmail.com> wrote:
Using basename works, but leaves the extra stuff at the end.
Which would have to be removed with another line of code

I get this--> ppbhat.tga";


if you're trying to parse Maya files, maybe you should start
by writing a simple Maya parser, and use that to extract the
relevant strings, *before* passing them to os.path.baseline?

</F>

Sep 6 '05 #5
Good Idea I'll try that!

Thanks for your assistance.
/\/\

Sep 7 '05 #6
sheffdog wrote:
Using regular expressions, the best I can do so far is using the re.sub
command but it still takes two lines. Can I do this in one line? Or
should I be approaching this differently? All I want to end up with is
the file name "ppbhat.tga".


A regular expression to do what you want:
s = 'setAttr ".ftn" -type "string" /assets/chars/boya/geo/textures/lod1/ppbhat.tga";'
s = re.sub(r".*/(.*\.tga).*", r"\1", s)
s

'ppbhat.tga'

Is a regular expression the best solution?
That depends on what else you need to do with your data file.
Sep 8 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by lawrence | last post: by
2 posts views Thread by Muumac | last post: by
20 posts views Thread by Ravi | last post: by
5 posts views Thread by Maurice LING | last post: by
2 posts views Thread by Fuzzyman | last post: by
20 posts views Thread by plahey | last post: by
3 posts views Thread by Jesper Stocholm | last post: by
7 posts views Thread by Matthew Warren | last post: by
reply views Thread by gcreed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.