how .split a string and keep the "splited" part?

Become a member of the CGSociety

Connect, Share, and Learn with our Large Growing CG Art Community. It's Free!

THREAD CLOSED
 
Thread Tools Search this Thread Display Modes
  02 February 2011
PYTHON --> how .split a string and keep the "splited" part?

Hi!
So I have a string that I want to split but I need to keep the exact part that I'm splitting

for example:




  1. s_words = 'i need toKeepTHISword'
  2. splitWord = s_words.split('THIS')
  3. # Result: ['i need toKeep', 'word']


Result wanted : ['THIS']


It does not have to be with split function, it can be with whatever other solution may exist..

Last edited by mquinta : 02 February 2011 at 08:47 AM.
 
  02 February 2011
Well, if you are splitting by a certain element, you alway know this element, in your case "THIS".
So I suppose you want to do something different. But what exactly? Do you want to split a sequence of characters by upper/lowercase sequences?
 
  02 February 2011
So all you're really looking for is validation that THIS is in the string?

wanted='THIS'

if wanted in searchText:
doSomething

or:

import re
if re.search(searchText,wanted):
doSomething

Both of which will run the do function if wanted is in the searchText string, or you just return the initial wanted string. by the way, they're should be a tab before the doSomething, damn formatting on forums.
__________________
Founder & CEO : Red9Consultancy
Red9 StudioPack : Red9 StudioPack GitHub
Author: Autodesk Masterclass 2011 - 'Live Animation Binding'
 
  02 February 2011
while I too would probably go with Mark's first suggestion (depending on the exact context) there is also the .find() string method which will give you the character index the given string exists at (or -1 if it's not there)

charIndex = 'i need toKeepTHISword'.find('THIS')
# Result: 13
:nathaN
 
  02 February 2011
To find uppercase 'words' in a string, you could use something like this:

import re
s_words = 'i need toKeepTHISword'
re.findall('\B[A-Z]{2,}', s_words) 
# Result: ['THIS'] # 
__________________
Michiel
http://thnkr.com
 
  02 February 2011
Hi!
Thank you all for your answers!!
Hummm I'll do have to study and understand better the "re" module.. I see that you all used it...

I saw this other method in other forum with the index() function (if you have interest on it):
  1. >>> idx = 'i need toKeepTHISword'.index("THIS")
  2. >>> 'i need toKeepTHISword'[idx:idx+len("THIS")]
  3. 'THIS'
  4. >>>


Thanks, I think that with your suggestions I'll be able to continue with the script!
 
  02 February 2011
A couple of good links that I often refer back to for the re module:

http://docs.activestate.com/komodo/4.4/regex-intro.html
http://code.google.com/edu/language...xpressions.html

Gives you a good overview
__________________
Founder & CEO : Red9Consultancy
Red9 StudioPack : Red9 StudioPack GitHub
Author: Autodesk Masterclass 2011 - 'Live Animation Binding'
 
  02 February 2011
Thread automatically closed

This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.
__________________
CGTalk Policy/Legalities
Note that as CGTalk Members, you agree to the terms and conditions of using this website.
 
Thread Closed share thread



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
CGSociety
Society of Digital Artists
www.cgsociety.org

Powered by vBulletin
Copyright 2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump
Miscellaneous

All times are GMT. The time now is 08:33 PM.


Powered by vBulletin
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.