473,786 Members | 2,399 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2376
"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.basenam e("/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.b asename(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.baselin e?

</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
2525
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 > news:da7e68e8.0405300907.3c8eabf7@posting.google.com... > > This reg ex will find me any block of strings 40 or more characters in > > length without a white space, yes? > > > > {40} > > > > > > To get it to include tabs and newlines, do I to this?
2
8555
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 BASE64 encoded! When xml contains only one big over 4MB file in it, preg_match and preg_match_all doesnt find it! When included file size is nelow 4MB, preg functions works perfectly! But PHP manual says that there is no size limitations to...
20
5779
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" >>>c = extract(a,b) >>>print c "abcdefghijklmnop"
5
7104
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 they different from regular strings? I've tried to look up in the python documentation but it just vaguely brushes through by saying that python raw strings are prefixed with "r" or "R". In PLY, "+" is "r'\+'" but "-" is "r'-'", why is there an...
2
2475
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 I can use the Regular Expression "\d{5}" to test for exactly 5 digits. How can I add the option for the string to be empty or must I always test the value before calling the Regular Expression? Thanks
2
2379
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 works - but can someone confirm that this is consistent and safe ? (No lurking encode errors - I assume it is only a decode that is done, in which case is it safe on a system that has a non-ascii compatible default encoding ? OTOH it would seem to...
20
2265
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? making me do: x=r'c:\blah' '\\'
3
1950
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 is that it performs like an old, limb man :o( The method is: public static StringBuilder RemoveChars(StringBuilder dataToClean_, string illegalChars_) { // only try to remove chars if there is data to clean
7
2085
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 have solved my own problem. Is \' the only thing escaped in a raw string so you can place ' in a raw string?
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9492
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10163
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10108
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9960
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5532
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.