LiteSpeed Technologies
Download Download     Blog Blog     Wiki Wiki     Forum Forum     Store     Contact Contact    

Go Back   LiteSpeed Support Forums > External Applications > CGI/Perl/Python > Quick help! - Regex for splitting a string

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 11-17-2012, 06:21 PM
thx_for_sharing thx_for_sharing is offline
New Member
 
Join Date: Nov 2012
Posts: 2
Default Quick help! - Regex for splitting a string

Hello,
I want to split the string in following manner. Here are my strings for example:
-----
"Hello this is a string.-2.34 This is an example1 string."
"Hello this is another string.3.53 This is an example2 string."
-----
Please note that "" is a U+F8FF unicode character.

I want to have 3 broken parts of the string:
"Hello this is a string.","-2.34"," This is an example1 string."
"Hello this is another string.","3.53","This is an example2 string."

I have written a regex to split the string but using this I cannot get the numeric part that I want. (-2.34 in first string)
---------
My code:
------------------------
import re
import os
from django.utils.encoding import smart_str, smart_unicode

text = open(r"C:\data.txt").read()
text = text.decode('utf-8')
print(smart_str(text))

pat = re.compile(u"\uf8ff-*\d+\.*\d+")
newpart = pat.split(text)
firstpart = newpart[::1]

print ("first part of the string ----")
for f in firstpart:
f = smart_str(f)
print ("-----")
print f


---------
I am still a learner and I'm sure there is a better way of doing this. Please help!
Reply With Quote
 

Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 08:38 AM.



- Archive - Top
© Copyright 2003-2011 LiteSpeed Technologies, Inc. All rights reserved. Privacy Policy.