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
Old 02 February 2011   #1
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.
 
Old 02 February 2011   #2
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?
 
Old 02 February 2011   #3
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'
 
Old 02 February 2011   #4
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)

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

Code:
import re s_words = 'i need toKeepTHISword' re.findall('\B[A-Z]{2,}', s_words) # Result: ['THIS'] #
__________________
Michiel
http://thnkr.com
 
Old 02 February 2011   #6
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!
 
Old 02 February 2011   #7
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'
 
Old 02 February 2011   #8
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.
 
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 02:45 PM.


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