Quick help! - Regex for splitting a string

Discussion in 'CGI/Perl/Python' started by thx_for_sharing, Nov 18, 2012.

  1. thx_for_sharing

    thx_for_sharing New Member

    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')

    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!

Share This Page