Extracting text from variable
#1
Hi,

Bit of a strange question but I hope someone can help me with this.

What I have is a variable that's called: this.is.a.test.s54352.test
Now the, this.is.a.test part is dynamic and changes all the time also the character count changes. but that's the part I need to have.

So I want to strip: this.is.a.test.s54352.test TO this.is.a.test

Thank you for looking in to this.
moh-san


Reply
#2
Found it already, sorry to bother.

Code:
import re

string = "this.is.a.test.s54352.test"
string2 = [m.start() for m in re.finditer('.s', string)]
string2 = string[:+string2[0]]
string2 = string2.replace ("."," ")

#this is a test
Reply
#3
You know that the regular expression '.s' means "any character followed by an s-charachter" and not "dot followed by s"?

Anyway, if you just search for any string bevore a given pattern (dot followed by s) you could do it in much other (simpler) ways:

Code:
string = "this.is.a.test.s54352.test"
print string.split('.s', 1)[0]
print string[:string.find('.s')]

And there are dozens more using regular expressions right.

It absolutely depends on the strings to search in, patterns to find with and finally the substring you look for Smile

Maybe next time give more than one example or describe the scheme...

regards,
sphere
My GitHub. My Add-ons:
Image
Reply

Logout Mark Read Team Forum Stats Members Help
Extracting text from variable0