473,386 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 2357
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: lawrence | last post by:
"Garp" <garp7@no7.blueyonder.co.uk> wrote in message news:<_vpuc.1424$j_3.13346038@news-text.cableinet.net>... > "lawrence" <lkrubner@geocities.com> wrote in message >...
2
by: Muumac | last post by:
I have problem with large textfiles! When I load over 4MB xml and then try to preg_match something in this I get always FALSE! I have <File>....</File> tags in XML. Between tags is files contents...
20
by: Ravi | last post by:
Hi, I have about 200GB of data that I need to go through and extract the common first part of a line. Something like this. >>>a = "abcdefghijklmnopqrstuvwxyz" >>>b = "abcdefghijklmnopBHLHT"...
5
by: Maurice LING | last post by:
I'm trying to toy around with PLY (python lex-yacc) by David Beazley from University of Chicago and realized that the lex module uses python raw strings. What are python raw strings and how are...
2
by: cleo | last post by:
I'm experimenting with Regular Expressions and Windows Forms. Frequently I want a value to be either a valid pattern or empty. For example a Zip code must be 5 digits or may be empty. I know that...
2
by: Fuzzyman | last post by:
Hello all, Can someone confirm that compiled regular expressions from ascii strings will always (and safely) yield unicode values when matched against unicode strings ? I've tested it and it...
20
by: plahey | last post by:
I thought I understood raw strings, then I got bitten by this: x=r'c:\blah\' which is illegal! I thought that \ had no special meanning in a raw string so why can't it be the last character?...
3
by: Jesper Stocholm | last post by:
I have developed a data-cleaner that extracts some data from a database, cleans it for illegal/unwanted data and writes it to a CSV-fil for later insertion to a SQL Server 2000 database. My problem...
7
by: Matthew Warren | last post by:
Hi, I would expect this to work, rawstring=r'some things\new things\some other things\' But it fails as the last backslash escapes the single quote. ...although writing this I think I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.